Class HttpURLConnectionImpl

java.lang.Object
java.net.URLConnection
java.net.HttpURLConnection
com.squareup.okhttp.internal.http.HttpURLConnectionImpl
All Implemented Interfaces:
Policy

public class HttpURLConnectionImpl
extends HttpURLConnection
implements Policy
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. Test the connection field on this class for null/non-null to determine of an instance is currently connected to a server.
  • Field Details

    • httpEngineFailure

      protected IOException httpEngineFailure
    • httpEngine

      protected HttpEngine httpEngine
  • Constructor Details

    • HttpURLConnectionImpl

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

    • 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
    • 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
    • getHttpConnectionToCache

      public HttpURLConnection getHttpConnectionToCache()
      Description copied from interface: Policy
      Returns the HttpURLConnection instance to store in the cache.
      Specified by:
      getHttpConnectionToCache in interface Policy
    • getHttpEngine

      public HttpEngine getHttpEngine()
    • getFixedContentLength

      public final long getFixedContentLength()
      Specified by:
      getFixedContentLength in interface Policy
      See Also:
      HttpURLConnection.setFixedLengthStreamingMode(int)
    • getChunkLength

      public final int getChunkLength()
      Specified by:
      getChunkLength in interface Policy
      See Also:
      HttpURLConnection.setChunkedStreamingMode(int)
    • usingProxy

      public final boolean usingProxy()
      Description copied from class: HttpURLConnection
      Returns whether this connection uses a proxy server or not.
      Specified by:
      usingProxy in interface Policy
      Specified by:
      usingProxy in class HttpURLConnection
      Returns:
      true if this connection passes a proxy server, false otherwise.
    • getResponseMessage

      public String getResponseMessage() throws IOException
      Description copied from class: HttpURLConnection
      Returns the response message returned by the remote HTTP server.
      Overrides:
      getResponseMessage in class HttpURLConnection
      Returns:
      the response message. null if no such response exists.
      Throws:
      IOException - if there is an error during the retrieval.
      See Also:
      HttpURLConnection.getResponseCode()
    • getResponseCode

      public final int getResponseCode() throws IOException
      Description copied from class: HttpURLConnection
      Returns the response code returned by the remote HTTP server.
      Overrides:
      getResponseCode in class HttpURLConnection
      Returns:
      the response code, -1 if no valid response code.
      Throws:
      IOException - if there is an IO error during the retrieval.
      See Also:
      HttpURLConnection.getResponseMessage()
    • 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.
    • 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
    • setFixedLengthStreamingMode

      public void setFixedLengthStreamingMode​(long contentLength)
      Description copied from class: HttpURLConnection
      Configures this connection to stream the request body with the known fixed byte count of contentLength.
      Overrides:
      setFixedLengthStreamingMode in class HttpURLConnection
      Parameters:
      contentLength - the fixed length of the HTTP request body.
      See Also:
      HttpURLConnection.setChunkedStreamingMode(int)
    • setSelectedProxy

      public final void setSelectedProxy​(Proxy proxy)
      Description copied from interface: Policy
      Sets the current proxy that this connection is using.
      Specified by:
      setSelectedProxy in interface Policy
      See Also:
      HttpURLConnection.usingProxy()