Class Connection

java.lang.Object
com.squareup.okhttp.Connection
All Implemented Interfaces:
Closeable, AutoCloseable

public final class Connection
extends Object
implements Closeable
Holds the sockets and streams of an HTTP, HTTPS, or HTTPS+SPDY connection, which may be used for multiple HTTP request/response exchanges. Connections may be direct to the origin server or via a proxy.

Typically instances of this class are created, connected and exercised automatically by the HTTP client. Applications may use this class to monitor HTTP connections as members of a connection pool.

Do not confuse this class with the misnamed HttpURLConnection, which isn't so much a connection as a single request/response exchange.

Modern TLS

There are tradeoffs when selecting which options to include when negotiating a secure connection to a remote host. Newer TLS options are quite useful:
  • Server Name Indication (SNI) enables one IP address to negotiate secure connections for multiple domain names.
  • Next Protocol Negotiation (NPN) enables the HTTPS port (443) to be used for both HTTP and SPDY transports.
Unfortunately, older HTTPS servers refuse to connect when such options are presented. Rather than avoiding these options entirely, this class allows a connection to be attempted with modern options and then retried without them should the attempt fail.
  • Constructor Details

    • Connection

      public Connection​(Route route)
  • Method Details

    • connect

      public void connect​(int connectTimeout, int readTimeout, TunnelRequest tunnelRequest) throws IOException
      Throws:
      IOException
    • isConnected

      public boolean isConnected()
      Returns true if connect(int, int, com.squareup.okhttp.TunnelRequest) has been attempted on this connection.
    • close

      public void close() throws IOException
      Description copied from interface: Closeable
      Closes the object and release any system resources it holds.

      Although only the first call has any effect, it is safe to call close multiple times on the same object. This is more lenient than the overridden AutoCloseable.close(), which may be called at most once.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Throws:
      IOException
    • getRoute

      public Route getRoute()
      Returns the route used by this connection.
    • getSocket

      public Socket getSocket()
      Returns the socket that this connection uses, or null if the connection is not currently connected.
    • isAlive

      public boolean isAlive()
      Returns true if this connection is alive.
    • isReadable

      public boolean isReadable()
      Returns true if we are confident that we can read data from this connection. This is more expensive and more accurate than isAlive(); callers should check isAlive() first.
    • resetIdleStartTime

      public void resetIdleStartTime()
    • isIdle

      public boolean isIdle()
      Returns true if this connection is idle.
    • isExpired

      public boolean isExpired​(long keepAliveDurationNs)
      Returns true if this connection has been idle for longer than keepAliveDurationNs.
    • getIdleStartTimeNs

      public long getIdleStartTimeNs()
      Returns the time in ns when this connection became idle. Undefined if this connection is not idle.
    • newTransport

      public Object newTransport​(HttpEngine httpEngine) throws IOException
      Returns the transport appropriate for this connection.
      Throws:
      IOException
    • isSpdy

      public boolean isSpdy()
      Returns true if this is a SPDY connection. Such connections can be used in multiple HTTP requests simultaneously.
    • getSpdyConnection

      public SpdyConnection getSpdyConnection()
    • getHttpMinorVersion

      public int getHttpMinorVersion()
      Returns the minor HTTP version that should be used for future requests on this connection. Either 0 for HTTP/1.0, or 1 for HTTP/1.1. The default value is 1 for new connections.
    • setHttpMinorVersion

      public void setHttpMinorVersion​(int httpMinorVersion)
    • requiresTunnel

      public boolean requiresTunnel()
      Returns true if the HTTP connection needs to tunnel one protocol over another, such as when using HTTPS through an HTTP proxy. When doing so, we must avoid buffering bytes intended for the higher-level protocol.
    • updateReadTimeout

      public final void updateReadTimeout​(int newTimeout) throws IOException
      Throws:
      IOException