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);
Share:
25,127
Mohamad
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, 2022

Comments

  • Mohamad
    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.

    1. How do I set an effective connection timeout?
    2. 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
    Mohamad over 12 years
    Thanks. 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. Porbably org.apache.http.params.* but where does the params argument you supply to setConnectionTimeout come from? I can't throw it in there, otherwise it would throw a params 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
    Seth over 12 years
    HttpParams params = new BasicHttpParams(); There's some more information here: hc.apache.org/httpcomponents-client-ga/tutorial/html/index.h‌​tml and the javadoc is here: hc.apache.org/httpcomponents-core-ga/httpcore/apidocs/org/…