Package com.squareup.okhttp
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.
-
Constructor Summary
Constructors Constructor Description Connection(Route route) -
Method Summary
Modifier and Type Method Description voidclose()Closes the object and release any system resources it holds.voidconnect(int connectTimeout, int readTimeout, TunnelRequest tunnelRequest)intgetHttpMinorVersion()Returns the minor HTTP version that should be used for future requests on this connection.longgetIdleStartTimeNs()Returns the time in ns when this connection became idle.RoutegetRoute()Returns the route used by this connection.SocketgetSocket()Returns the socket that this connection uses, or null if the connection is not currently connected.SpdyConnectiongetSpdyConnection()booleanisAlive()Returns true if this connection is alive.booleanisConnected()Returns true ifconnect(int, int, com.squareup.okhttp.TunnelRequest)has been attempted on this connection.booleanisExpired(long keepAliveDurationNs)Returns true if this connection has been idle for longer thankeepAliveDurationNs.booleanisIdle()Returns true if this connection is idle.booleanisReadable()Returns true if we are confident that we can read data from this connection.booleanisSpdy()Returns true if this is a SPDY connection.ObjectnewTransport(HttpEngine httpEngine)Returns the transport appropriate for this connection.booleanrequiresTunnel()Returns true if the HTTP connection needs to tunnel one protocol over another, such as when using HTTPS through an HTTP proxy.voidresetIdleStartTime()voidsetHttpMinorVersion(int httpMinorVersion)voidupdateReadTimeout(int newTimeout)
-
Constructor Details
-
Connection
-
-
Method Details
-
connect
public void connect(int connectTimeout, int readTimeout, TunnelRequest tunnelRequest) throws IOException- Throws:
IOException
-
isConnected
public boolean isConnected()Returns true ifconnect(int, int, com.squareup.okhttp.TunnelRequest)has been attempted on this connection. -
close
Description copied from interface:CloseableCloses 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:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
getRoute
Returns the route used by this connection. -
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() -
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 thankeepAliveDurationNs. -
getIdleStartTimeNs
public long getIdleStartTimeNs()Returns the time in ns when this connection became idle. Undefined if this connection is not idle. -
newTransport
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
-
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
- Throws:
IOException
-