http://blog.jayway.com/2009/03/17/configuring-timeout-with-apache-httpclient-40/
Configuring Timeout with Apache HttpClient 4.0
March 17th, 2009 by Mattias Hellborg Arthursson — Tips & Tricks
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
Tags: 4.0, apache, configuration, httpclient, programming, timeout
12 comments ↓
- #1 BoD on 03.28.09 at 2:38
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 on 03.30.09 at 15:42
- #3 Oguen on 04.08.09 at 11:16
Thank you, exactly what i was looking for !
- #4 Peter on 06.15.09 at 1:42
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.
- #5 Mattias Hellborg Arthursson on 06.15.09 at 12:09
- #6 Bram Luyten on 12.14.10 at 18:03
just what I was looking for, thanks for blogging Mattias
- #7 Thuy on 02.17.11 at 0:02
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 on 02.25.11 at 10:32
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 steve on 06.05.11 at 21:26
Just found this from a google search. Just a quick message to say thanks, worked perfectly here
- #10 Anon on 06.21.11 at 17:37
Likewise, just to say it worked and to thank you!
- #11 Troy on 10.03.11 at 0:29
@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 mente on 10.11.11 at 13:07
For spring integration you can use HttpConnectionParamBean