Apache Java HttpClient not re-using persistent connections (solved)

Currently trying to diagnose some .NET connection failures so I ported my code to Java to see if I could eliminate some causes as this won’t use SChannel (instead using JSSE).

To do this I chose the Apache HttpClient (version 4.3) – running multiple calls it seemed to be working but quite slowly. Wireshark revealed that it was doing a handshake every time. See multiple client hellos:


TCPView also showed multiple connections being established:


Despite trying BasicHttpClientConnectionManager and PoolingHttpClientConnectionManager with a pool size of 1 it still wouldn’t reuse the connection. Keep-Alive headers were being sent by the server but as shown in the SSL diagnostics (using -Djavax.net.debug=all), the socket was still closed:

Keep-Alive: timeout=10, max=100, Connection: Keep-Alive]}
main, called close()
main, called closeInternal(true)
main, SEND TLSv1.1 ALERT: warning, description = close_notify

In my scenario, I expected the response from the server to only contain a location header and no body however I then noticed was the content-length was set to 20.


So the problem was that I was not fully consuming/reading the body of the response, doing so meant the socket could be re-used:

HttpEntity entity = response.getEntity();
// do something useful with the response body
// and ensure it is fully consumed otherwise connection is not reused

Now I get a much nicer looking wireshark trace!


Of course now I know the answer – I can also find it on stackoverflow: http://stackoverflow.com/questions/8200038/httpclient-4-x-connection-reuse-not-happening


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s