Apache HttpComponents HttpClient timeout

Landon Kuhn picture Landon Kuhn · May 17, 2011 · Viewed 60.7k times · Source

How do I set the connection timeout in httpcomponents httpclient? I have found the documentation at: http://hc.apache.org/httpcomponents-client-ga/tutorial/html/connmgmt.html but it is not clear how these parameters are actually set.

Also, an explanation of the difference between SO_TIMEOUT and CONNECTION_TIMEOUT would be helpful.

Answer

30thh picture 30thh · Nov 7, 2013

In version 4.3 of Apache Http Client the configuration was refactored (again). The new way looks like this:

RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(timeout);
requestBuilder.setConnectionRequestTimeout(timeout);

HttpClientBuilder builder = HttpClientBuilder.create();     
builder.setDefaultRequestConfig(requestBuilder.build());
HttpClient client = builder.build();