Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a connection timeout in Apache http client? [duplicate]

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
like image 311
Mohamad Avatar asked Oct 15 '11 14:10

Mohamad


People also ask

How do I set HttpClient timeout?

The default value is 100,000 milliseconds (100 seconds). To set an infinite timeout, set the property value to InfiniteTimeSpan. A Domain Name System (DNS) query may take up to 15 seconds to return or time out.

What is connection timeout in HttpClient?

timeout) – the time waiting for data – after establishing the connection; maximum time of inactivity between two data packets. the Connection Manager Timeout (http. connection-manager. timeout) – the time to wait for a connection from the connection manager/pool.

What is the difference between connection timeout and connection request timeout?

request timeout — a time period required to process an HTTP call: from sending a request to receiving a response. connection timeout — a time period in which a client should establish a connection with a server.

What is the default timeout for HTTP request in Java?

The Request Timeout property specifies the number of seconds the server waits between accepting a connection to a client and receiving information from it. The default setting is 30 seconds.


2 Answers

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);
like image 162
Seth Avatar answered Oct 18 '22 21:10

Seth


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);
like image 31
Ryhan Avatar answered Oct 18 '22 20:10

Ryhan