Class HttpConnection


  • public final class HttpConnection
    extends Object
    A socket connection that can be used to send HTTP/1.1 messages. This class strictly enforces the following lifecycle:
    1. Send request headers.
    2. Open a sink to write the request body. Either fixed-length or chunked.
    3. Write to and then close that sink.
    4. Read response headers.
    5. Open a source to read the response body. Either fixed-length, chunked or unknown length.
    6. Read from and close that source.

    Exchanges that do not have a request body may skip creating and closing the request body. Exchanges that do not have a response body can call newFixedLengthSource(0) and may skip reading and closing that source.

    • Method Detail

      • setTimeouts

        public void setTimeouts​(int readTimeoutMillis,
                                int writeTimeoutMillis)
      • poolOnIdle

        public void poolOnIdle()
        Configure this connection to put itself back into the connection pool when the HTTP response body is exhausted.
      • closeOnIdle

        public void closeOnIdle()
                         throws IOException
        Configure this connection to close itself when the HTTP response body is exhausted.
        Throws:
        IOException
      • isClosed

        public boolean isClosed()
        Returns true if this connection is closed.
      • bufferSize

        public long bufferSize()
        Returns the number of buffered bytes immediately readable.
      • isReadable

        public boolean isReadable()
        Test for a stale socket.
      • writeRequest

        public void writeRequest​(Headers headers,
                                 String requestLine)
                          throws IOException
        Returns bytes of a request header for sending on an HTTP transport.
        Throws:
        IOException
      • newChunkedSink

        public Sink newChunkedSink()
      • newFixedLengthSink

        public Sink newFixedLengthSink​(long contentLength)