Java Reference
In-Depth Information
problematic for encrypted HTTPS connections using SSL or TLS, because the hand‐
shake to set up a secure socket is substantially more work than setting up a regular
socket.
In HTTP 1.1 and later, the server doesn't have to close the socket after it sends its re‐
sponse. It can leave it open and wait for a new request from the client on the same socket.
Multiple requests and responses can be sent in series over a single TCP connection.
However, the lockstep pattern of a client request followed by a server response remains
the same.
A client indicates that it's willing to reuse a socket by including a
Connection
field in the
HTTP request header with the value
Keep-Alive
:
Connection: Keep-Alive
The
URL
class transparently supports HTTP Keep-Alive unless explicitly turned off. That
is, it will reuse a socket if you connect to the same server again before the server has
closed the connection. You can control Java's use of HTTP Keep-Alive with several
system properties:
• Set
http.keepAlive
to “true or false” to enable/disable HTTP Keep-Alive. (It is
enabled by default.)
• Set
http.maxConnections
to the number of sockets you're willing to hold open at
one time. The default is 5.
• Set
http.keepAlive.remainingData
to true to let Java clean up after abandoned
connections (Java 6 or later). It is false by default.
• Set
sun.net.http.errorstream.enableBuffering
to true to attempt to buffer the
relatively short error streams from 400- and 500-level responses, so the connection
can be freed up for reuse sooner. It is false by default.
• Set
sun.net.http.errorstream.bufferSize
to the number of bytes to use for
buffering error streams. The default is 4,096 bytes.
• Set
sun.net.http.errorstream.timeout
to the number of milliseconds before
timing out a read from the error stream. It is 300 milliseconds by default.
The defaults are reasonable, except that you probably do want to set
sun.net.http.er
rorstream.enableBuffering
to true unless you want to read the error streams from
failed requests.