Great news everyone: just recently an all-new version of Apache HttpClient was released. HttpClient is now part of the new initiative Apache HttpComponents, which seems to aim for a complete approach to Http programming in Java; server side as well as client side.
I've used Commons HttpClient in older versions on several occasions in the past and have found it to be extremely useful. I've only just started working with the new version, but there seems to quite a lot of good stuff in there. For example I'm thrilled to see that the Template pattern, heavily used in Spring, has now made its way to the Apache world (e.g. ResponseHandler, a callback interface to be used with HttpClient for handling results).
Now, configuration of HttpClient
has always been a little bit tricky. There are so many features in there, and most of these are configured using generic parameters (HttpParams
), basically name-value pairs of a setting identifier and its value. This has not been made that much easier in the new version.
In my current project I was struggling to find the appropriate parameters for configuring timeouts with the new version of the framework. It turned out to be quite difficult to find clear information on this - the documentation on the 4.0 version of HttpClient
is still quite sparse. Since it took me a while to find out how to do this I figured I wasn't alone, so here goes:
My original solution for this problem - being completely unable to find the proper way to do it - was to use the hard-coded property values and set them as parameters in HttpParams
. Fortunately, a reader with signature 'BoD' (see below) knew the right way to do this and was kind enough to share.
Right, the code looks as follows:
DefaultHttpClient httpClient = new DefaultHttpClient(); HttpParams params = httpClient.getParams(); HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis); HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
Simple as that - hard to understand why that should take me so long to figure out . Thanks again to 'BoD' for setting me straight on this.
Mattias Hellborg Arthursson
Consultant at Jayway