Skip to main content


Showing posts from February, 2015

HttpClient 4.x Timeout

HttpClient is one of the most versatile Java libraries. Unfortunately, it comes with a lot of configuration options that may be way too cryptic or difficult. While the API for 4.x series has been significantly improved there are still some sharp edges.

The deprecated or 3.x way of setting the timeout. This is done using params. Note that this is still 4.x code but a deprecated one.
DefaultHttpClient httpClient = ...; HttpParams httpParams = httpClient.getParams(); httpParams.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 1000L); httpParams.setParameter(CoreConnectionPNames.SO_TIMEOUT, 1000L); httpParams.setParameter(ClientPNames.CONN_MANAGER_TIMEOUT, 1000L); Now the part httpClient.getParams() is deprecated since obviously this is a nasty API. You need to keep track of parameters with Enums/constants and their type as well.

The right 4.x way aka the builder way
HttpClient 4 is full of builder for everything. While I often love the Builder patterns in some cases the testi…