Class HttpURLConnectionImpl


  • public class HttpURLConnectionImpl
    extends HttpURLConnection
    This implementation uses HttpEngine to send requests and receive responses. This class may use multiple HttpEngines to follow redirects, authentication retries, etc. to retrieve the final response body.

    What does 'connected' mean?

    This class inherits a connected field from the superclass. That field is not used to indicate not whether this URLConnection is currently connected. Instead, it indicates whether a connection has ever been attempted. Once a connection has been attempted, certain properties (request header fields, request method, etc.) are immutable.
    • Constructor Detail

      • HttpURLConnectionImpl

        public HttpURLConnectionImpl​(URL url,
                                     OkHttpClient client)
    • Method Detail

      • connect

        public final void connect()
                           throws IOException
        Description copied from class: URLConnection
        Opens a connection to the resource. This method will not reconnect to a resource after the initial connection has been closed.
        Specified by:
        connect in class URLConnection
        Throws:
        IOException - if an error occurs while connecting to the resource.
      • disconnect

        public final void disconnect()
        Description copied from class: HttpURLConnection
        Releases this connection so that its resources may be either reused or closed.

        Unlike other Java implementations, this will not necessarily close socket connections that can be reused. You can disable all connection reuse by setting the http.keepAlive system property to false before issuing any HTTP requests.

        Specified by:
        disconnect in class HttpURLConnection
      • getErrorStream

        public final InputStream getErrorStream()
        Returns an input stream from the server in the case of error such as the requested file (txt, htm, html) is not found on the remote server.
        Overrides:
        getErrorStream in class HttpURLConnection
        Returns:
        the error input stream returned by the server.
      • getHeaderField

        public final String getHeaderField​(int position)
        Returns the value of the field at position. Returns null if there are fewer than position headers.
        Overrides:
        getHeaderField in class URLConnection
        Parameters:
        position - the field position of the response header.
        Returns:
        the value of the field at position pos.
      • getHeaderField

        public final String getHeaderField​(String fieldName)
        Returns the value of the field corresponding to the fieldName, or null if there is no such field. If the field has multiple values, the last value is returned.
        Overrides:
        getHeaderField in class URLConnection
        Parameters:
        fieldName - the name of the header field.
        Returns:
        the value of the header field.
      • getHeaderFieldKey

        public final String getHeaderFieldKey​(int position)
        Description copied from class: URLConnection
        Returns the name of the header field at the given position posn or null if there are fewer than posn fields. The base implementation of this method returns always null.

        Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.

        Overrides:
        getHeaderFieldKey in class URLConnection
        Parameters:
        position - the position of the header field which has to be returned.
        Returns:
        the header field name at the given position.
      • getHeaderFields

        public final Map<String,​List<String>> getHeaderFields()
        Description copied from class: URLConnection
        Returns an unmodifiable map of the response-header fields and values. The response-header field names are the key values of the map. The map values are lists of header field values associated with a particular key name.

        Some implementations (notably HttpURLConnection) include a mapping for the null key; in HTTP's case, this maps to the HTTP status line and is treated as being at position 0 when indexing into the header fields.

        Overrides:
        getHeaderFields in class URLConnection
        Returns:
        the response-header representing generic map.
      • getRequestProperties

        public final Map<String,​List<String>> getRequestProperties()
        Description copied from class: URLConnection
        Returns an unmodifiable map of general request properties used by this connection. The request property names are the key values of the map. The map values are lists of property values of the corresponding key name.
        Overrides:
        getRequestProperties in class URLConnection
        Returns:
        the request-property representing generic map.
      • getInputStream

        public final InputStream getInputStream()
                                         throws IOException
        Description copied from class: URLConnection
        Returns an InputStream for reading data from the resource pointed by this URLConnection. It throws an UnknownServiceException by default. This method must be overridden by its subclasses.
        Overrides:
        getInputStream in class URLConnection
        Returns:
        the InputStream to read data from.
        Throws:
        IOException - if no InputStream could be created.
      • getOutputStream

        public final OutputStream getOutputStream()
                                           throws IOException
        Description copied from class: URLConnection
        Returns an OutputStream for writing data to this URLConnection. It throws an UnknownServiceException by default. This method must be overridden by its subclasses.
        Overrides:
        getOutputStream in class URLConnection
        Returns:
        the OutputStream to write data.
        Throws:
        IOException - if no OutputStream could be created.
      • getPermission

        public final Permission getPermission()
                                       throws IOException
        Description copied from class: HttpURLConnection
        Returns the permission object (in this case SocketPermission) with the host and the port number as the target name and "resolve, connect" as the action list. If the port number of this URL instance is lower than 0 the port will be set to 80.
        Overrides:
        getPermission in class HttpURLConnection
        Returns:
        the permission object required for this connection.
        Throws:
        IOException - if an IO exception occurs during the creation of the permission object.
      • getRequestProperty

        public final String getRequestProperty​(String field)
        Description copied from class: URLConnection
        Returns the value of the request header property specified by {code field} or null if there is no field with this name. The base implementation of this method returns always null.
        Overrides:
        getRequestProperty in class URLConnection
        Parameters:
        field - the name of the request header property.
        Returns:
        the value of the property.
      • setConnectTimeout

        public void setConnectTimeout​(int timeoutMillis)
        Description copied from class: URLConnection
        Sets the maximum time in milliseconds to wait while connecting. Connecting to a server will fail with a SocketTimeoutException if the timeout elapses before a connection is established. The default value of 0 causes us to do a blocking connect. This does not mean we will never time out, but it probably means you'll get a TCP timeout after several minutes.

        Warning: if the hostname resolves to multiple IP addresses, this client will try each in RFC 3484 order. If connecting to each of these addresses fails, multiple timeouts will elapse before the connect attempt throws an exception. Host names that support both IPv6 and IPv4 always have at least 2 IP addresses.

        Overrides:
        setConnectTimeout in class URLConnection
      • setInstanceFollowRedirects

        public void setInstanceFollowRedirects​(boolean followRedirects)
        Description copied from class: HttpURLConnection
        Sets whether this connection follows redirects.
        Overrides:
        setInstanceFollowRedirects in class HttpURLConnection
        Parameters:
        followRedirects - true if this connection will follows redirects, false otherwise.
      • getConnectTimeout

        public int getConnectTimeout()
        Description copied from class: URLConnection
        Returns the connect timeout in milliseconds. (See {#setConnectTimeout}.)
        Overrides:
        getConnectTimeout in class URLConnection
      • setReadTimeout

        public void setReadTimeout​(int timeoutMillis)
        Description copied from class: URLConnection
        Sets the maximum time to wait for an input stream read to complete before giving up. Reading will fail with a SocketTimeoutException if the timeout elapses before data becomes available. The default value of 0 disables read timeouts; read attempts will block indefinitely.
        Overrides:
        setReadTimeout in class URLConnection
        Parameters:
        timeoutMillis - the read timeout in milliseconds. Non-negative.
      • getReadTimeout

        public int getReadTimeout()
        Description copied from class: URLConnection
        Returns the read timeout in milliseconds, or 0 if reads never timeout.
        Overrides:
        getReadTimeout in class URLConnection
      • usingProxy

        public final boolean usingProxy()
        Returns true if either:
        • A specific proxy was explicitly configured for this connection.
        • The response has already been retrieved, and a proxy was selected in order to get it.

        Warning: This method may return false before attempting to connect and true afterwards.

        Specified by:
        usingProxy in class HttpURLConnection
        Returns:
        true if this connection passes a proxy server, false otherwise.
      • setRequestProperty

        public final void setRequestProperty​(String field,
                                             String newValue)
        Description copied from class: URLConnection
        Sets the value of the specified request header field. The value will only be used by the current URLConnection instance. This method can only be called before the connection is established.
        Overrides:
        setRequestProperty in class URLConnection
        Parameters:
        field - the request header field to be set.
        newValue - the new value of the specified property.
      • setIfModifiedSince

        public void setIfModifiedSince​(long newValue)
        Description copied from class: URLConnection
        Sets the point of time since when the data must be modified to be transmitted. Some protocols transmit data only if it has been modified more recently than a particular time. The data will be transmitted regardless of its timestamp if this option is set to 0.
        Overrides:
        setIfModifiedSince in class URLConnection
        Parameters:
        newValue - the time in milliseconds since January 1, 1970 GMT.
        See Also:
        URLConnection.ifModifiedSince
      • addRequestProperty

        public final void addRequestProperty​(String field,
                                             String value)
        Description copied from class: URLConnection
        Adds the given property to the request header. Existing properties with the same name will not be overwritten by this method.
        Overrides:
        addRequestProperty in class URLConnection
        Parameters:
        field - the request property field name to add.
        value - the value of the property which is to add.
      • setFixedLengthStreamingMode

        public void setFixedLengthStreamingMode​(int contentLength)
        Description copied from class: HttpURLConnection
        Equivalent to setFixedLengthStreamingMode((long) contentLength), but available on earlier versions of Android and limited to 2 GiB.
        Overrides:
        setFixedLengthStreamingMode in class HttpURLConnection