Class HttpURLConnection

    • Constructor Detail

      • HttpURLConnection

        public HttpURLConnection​(URL u)
        this constructor is used by other protocol handlers such as ftp that want to use http to fetch urls on their behalf.
      • HttpURLConnection

        protected HttpURLConnection​(URL u,
                                    Handler handler)
    • Method Detail

      • setNewClient

        protected void setNewClient​(URL url)
                             throws IOException
        Obtain a HttpsClient object. Use the cached copy if specified.
        Parameters:
        url - the URL being accessed
        Throws:
        IOException
      • connect

        public void connect()
                     throws IOException
        Description copied from class: URLConnection
        Opens a communications link to the resource referenced by this URL, if such a connection has not already been established.

        If the connect method is called when the connection has already been opened (indicated by the connected field having the value true), the call is ignored.

        URLConnection objects go through two phases: first they are created, then they are connected. After being created, and before being connected, various options can be specified (e.g., doInput and UseCaches). After connecting, it is an error to try to set them. Operations that depend on being connected, like getContentLength, will implicitly perform the connection, if necessary.

        Specified by:
        connect in class URLConnection
        Throws:
        IOException - if an I/O error occurs while opening the connection.
        See Also:
        URLConnection.connected, URLConnection.getConnectTimeout(), URLConnection.setConnectTimeout(int)
      • streaming

        public boolean streaming()
      • getErrorStream

        @Nullable
        public InputStream getErrorStream()
        Description copied from class: HttpURLConnection
        Returns the error stream if the connection failed but the server sent useful data nonetheless. The typical example is when an HTTP server responds with a 404, which will cause a FileNotFoundException to be thrown in connect, but the server sent an HTML help page with suggestions as to what to do.

        This method will not cause a connection to be initiated. If the connection was not connected, or if the server did not have an error while connecting or if the server had an error but no error data was sent, this method will return null. This is the default.

        Overrides:
        getErrorStream in class HttpURLConnection
        Returns:
        an error stream if any, null if there have been no errors, the connection is not connected or the server sent no useful data.
      • usingProxy

        public boolean usingProxy()
        Description copied from class: HttpURLConnection
        Indicates if the connection is going through a proxy.
        Specified by:
        usingProxy in class HttpURLConnection
        Returns:
        a boolean indicating if the connection is using a proxy.
      • getHeaderField

        @Nullable
        public String getHeaderField​(String name)
        Gets a header field by name. Returns null if not known.
        Overrides:
        getHeaderField in class URLConnection
        Parameters:
        name - the name of the header field
        Returns:
        the value of the named header field, or null if there is no such field in the header.
      • getHeaderFields

        public Map<String,​List<String>> getHeaderFields()
        Returns an unmodifiable Map of the header fields. The Map keys are Strings that represent the response-header field names. Each Map value is an unmodifiable List of Strings that represents the corresponding field values.
        Overrides:
        getHeaderFields in class URLConnection
        Returns:
        a Map of header fields
        Since:
        1.4
      • getHeaderFieldKey

        @Nullable
        public String getHeaderFieldKey​(int n)
        Gets a header field by index. Returns null if not known.
        Overrides:
        getHeaderFieldKey in class HttpURLConnection
        Parameters:
        n - the index of the header field
        Returns:
        the key for the nth header field, or null if the key does not exist.
      • addRequestProperty

        public void addRequestProperty​(String key,
                                       String value)
        Adds a general request property specified by a key-value pair. This method will not overwrite existing values associated with the same key.
        Overrides:
        addRequestProperty in class URLConnection
        Parameters:
        key - the keyword by which the request is known (e.g., "accept").
        value - the value associated with it.
        Since:
        1.4
        See Also:
        getRequestProperties()
      • setAuthenticationProperty

        public void setAuthenticationProperty​(String key,
                                              String value)
      • getRequestProperties

        public Map<String,​List<String>> getRequestProperties()
        Returns an unmodifiable Map of general request properties for this connection. The Map keys are Strings that represent the request-header field names. Each Map value is a unmodifiable List of Strings that represents the corresponding field values.
        Overrides:
        getRequestProperties in class URLConnection
        Returns:
        a Map of the general request properties for this connection.
        Throws:
        IllegalStateException - if already connected
        Since:
        1.4
      • setConnectTimeout

        public void setConnectTimeout​(int timeout)
        Description copied from class: URLConnection
        Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the resource referenced by this URLConnection. If the timeout expires before the connection can be established, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

        Some non-standard implementation of this method may ignore the specified timeout. To see the connect timeout set, please call getConnectTimeout().

        Overrides:
        setConnectTimeout in class URLConnection
        Parameters:
        timeout - an int that specifies the connect timeout value in milliseconds
        See Also:
        URLConnection.getConnectTimeout(), URLConnection.connect()
      • setReadTimeout

        public void setReadTimeout​(int timeout)
        Sets the read timeout to a specified timeout, in milliseconds. A non-zero value specifies the timeout when reading from Input stream when a connection is established to a resource. If the timeout expires before there is data available for read, a java.net.SocketTimeoutException is raised. A timeout of zero is interpreted as an infinite timeout.

        Some non-standard implementation of this method ignores the specified timeout. To see the read timeout set, please call getReadTimeout().

        Overrides:
        setReadTimeout in class URLConnection
        Parameters:
        timeout - an int that specifies the timeout value to be used in milliseconds
        Throws:
        IllegalArgumentException - if the timeout parameter is negative
        Since:
        1.5
        See Also:
        InputStream.read()