Configuring Timeout with Apache HttpClient 4.0

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:

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.

18 Comments

  1. BoD

    A better version is:

    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
    HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
    HttpClient httpClient = new DefaultHttpClient(httpParams);

    Which does the same, but avoids using hard-coded strings that may even change with the library!

  2. Mattias Hellborg Arthursson

    Ah, excellent! That was just the thing I was looking for. I completely agree that the hard-coded stuff should be avoided; I was just completely unable to find out how to do it properly. Thanks.

  3. Thank you, exactly what i was looking for !

  4. Peter

    Well, If you guys are lazy and want auto generation of your Apache HttpClient code, use HTTP4E:

    It is an awesome Eclipse plugin. It has tabs, syntax coloring, auto suggest, and most importantly you can create an HTTP, REST call in 5 seconds. And when you have an HTTP call the one-click Java code generation just rocks. The code generated is pure Apache HttpClient code, as the tool is build on top of Apache HttpClient.

    You can use it for HTTP tampering, hacking, having fun with it.

    http://http4e.roussev.org/

  5. Mattias Hellborg Arthursson

    Well, I’m generally not too happy about generated code – if the API is too complicated to use with proper code the API should be simplified. Typically, HttpClient is in this category, which is why simplification is included in Spring’s RestTemplate (included in the upcoming Spring 3.0): http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/web/client/RestTemplate.html

    That said, I tried the plugin out just now and I really like the it for troubleshooting and testing out e.g. rest servers. Looks pretty sweet.

  6. just what I was looking for, thanks for blogging Mattias

  7. Thuy

    Thanks for the article. Just switched to apache httpclient 4.1. Just a correction on your code (should be params rather than httpParams) :

    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
    HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);

  8. Brijesh

    How can i specify source IP address for HttpClient ?
    My requirement is that i want to originate http request from specific Ip address.

    thanks

  9. Just found this from a google search. Just a quick message to say thanks, worked perfectly here

  10. Likewise, just to say it worked and to thank you!

  11. @Brijesh:

    Specifying source route:
    ConnRoutePNames.LOCAL_ADDRESS=’http.route.local-address’: defines a local address to be used by all default route planner. On machines with multiple network interfaces, this parameter can be used to select the network interface from which the connection originates. This parameter expects a value of type java.net.InetAddress. If this parameter is not set, a default local address will be used automatically.

  12. For spring integration you can use HttpConnectionParamBean

  13. Parminder

    Thanks you!

  14. LiaM

    How to set up a request timeout, meaning a read timeout on the whole request-response? Say if the response is so long that it keeps streaming, this timeout would be useful to abort the connection.

  15. maxim

    doesn’t works

  16. David

    What is the difference between params and httpParams in your code?

  17. rob

    Unfortunately, as of 4.3 getParams() (as well as DefaultHttpClient) is deprecated. Maybe it was too easy to use…

    Instead of getParams() the javadoc tells us to use RequestConfig – although it might not be instantly clear how that is supposed be a replacement…

    The wiki (http://wiki.apache.org/HttpComponents/HttpClientConfiguration) tells us how it (doesn’t) work:

    “Please note that HttpClient instances created with HttpClientBuilder or HttpClients [which is now the only valid way to create any instances] are immutable. Their configuration can no longer be altered.”

    So, the only way to go now is to create a RequestConfig and feed it to the _request_ via HttpRequestBase.setConfig.

    I have no idea why it was made to work like that, but that’s how it is…

  18. Jeferson

    Thank you, exactly what i was looking for !

Leave a Reply