Class Connection


  • public final class Connection
    extends Object
    The sockets and streams of an HTTP, HTTPS, or HTTPS+SPDY connection. 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.
    • Application Layer Protocol Negotiation (ALPN) enables the HTTPS port (443) to be used for different HTTP and SPDY protocols.
    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.
    • Method Detail

      • 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.
      • getHandshake

        public Handshake getHandshake()
      • getProtocol

        public Protocol getProtocol()
        Returns the protocol negotiated by this connection, or Protocol.HTTP_1_1 if no protocol has been negotiated.
      • toString

        public String toString()
        Description copied from class: Object
        Returns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:
           getClass().getName() + '@' + Integer.toHexString(hashCode())

        See Writing a useful toString method if you intend implementing your own toString method.

        Overrides:
        toString in class Object
        Returns:
        a printable representation of this object.