How do I set a connection timeout in Apache http client?
25,127
Solution 1
The documentation for apache's HttpClient is kind of spotty. Try this in your setup (it worked for me with version 4):
HttpConnectionParams.setConnectionTimeout(params, 10000);
HttpConnectionParams.setSoTimeout(params, 10000);
... set more parameters here if you want to ...
SchemeRegistry schemeRegistry = new SchemeRegistry();
.. do whatever you ant with the scheme registry here ...
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
client = new DefaultHttpClient(connectionManager, params);
Solution 2
You have to define a HttpParams object using the framework's class methods.
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
HttpConnectionParams.setConnectionTimeout(params, 2000);
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
registry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
HttpClient client = DefaultHttpClient(ccm, params);
Author by
Mohamad
I love well designed digital products, programming, and tech. Driving digital product roadmap in Brazil for world’s third largest retailer of home decor and building materials.
Updated on July 09, 2022Comments
-
Mohamad almost 2 years
I want to run thread safe, asynchronous HTTP requests using HTTPClient. I noticed that it does not respect my
CONNECTION_TIMEOUT
argument.The code is ColdFusion / Java hybrid.
client = loader.create("org.apache.http.impl.nio.client.DefaultHttpAsyncClient").init(); CoreConnectionPNames = loader.create("org.apache.http.params.CoreConnectionPNames"); client.getParams() .setIntParameter(JavaCast("string", CoreConnectionPNames.SO_TIMEOUT), 10) .setIntParameter(JavaCast("string", CoreConnectionPNames.CONNECTION_TIMEOUT), 10); client.start(); request = loader.create("org.apache.http.client.methods.HttpGet").init("http://www.google.com"); future = client.execute(request, javacast("null", "")); try { response = future.get(); } catch(e any) {} client.getConnectionManager().shutdown();
Regardless of what I supply for CONNECTION_TIMEOUT, the requests always return 200 OK. Check the output below.
- How do I set an effective connection timeout?
- Does CONNECTION_TIMEOUT do anything?
Output
200 OK http://www.google.com/ 200 OK http://www.google.com/ [snip] 5 requests using Async Client in: 2308 ms
-
Mohamad over 12 yearsThanks. I'm trying your code out, but it takes extra work to translate it to CF. So bare with me. In the first two linse
HTTPConnectionParams
refers to an object that I need to create. Porbablyorg.apache.http.params.*
but where does theparams
argument you supply tosetConnectionTimeout
come from? I can't throw it in there, otherwise it would throw aparams is undefined
error... sorry if this sounds obtuse, but working with java/CF hybrid code is tedious. Also, is the SchemeRegistry object necessary, can I just leave it out? -
Seth over 12 yearsHttpParams params = new BasicHttpParams(); There's some more information here: hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html and the javadoc is here: hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/…