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 EntityUtils.consume(entity);
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