3

I am new in Apache HttpClient, I used the following code to get the HTTP connection timeout (disconnected) after certain time interval:

PostMethod method = new PostMethod(authURL);
HttpClient client = new HttpClient();
HttpClientParams params= new HttpClientParams();
params.setParameter(params.CONNECTION_MANAGER_TIMEOUT, 10); //10 Nano second
client.executeMethod(method);

but it wait for more than one minute without any hope to timeout/disconnect? Where can the problem be?

mebada
  • 2,252
  • 4
  • 27
  • 35

3 Answers3

4

There are 2 timeouts involved in HTTPClient, try to set both,

  client.getHttpConnectionManager().
        getParams().setConnectionTimeout(5000);
  client.getHttpConnectionManager().
        getParams().setSoTimeout(5000);

However, the values will be ignored if the connection is stuck in a native socket call. So you might have to run the request in a different thread so you can time it out. See my answer to this question on how to do that,

java native Process timeout

Community
  • 1
  • 1
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
1

The connection manager timeout triggers when the act of trying to get a connection from your connection manager takes too long. This is not the same as the timeout for the http connection itself. Use HttpClientParams.setSoTimeout() instead.

http://hc.apache.org/httpclient-3.x/apidocs/org/apache/commons/httpclient/params/HttpMethodParams.html#setSoTimeout%28int%29

danben
  • 80,905
  • 18
  • 123
  • 145
  • thanks for your reply,,, I have tried the HttpClientParams.setSoTimeout() but the problem still there,ny more suggestions,,,, – mebada Dec 17 '09 at 23:09
0

Have you looked at setting SO_TIMEOUT ?

Sets the socket timeout (SO_TIMEOUT) in milliseconds to be used when executing the method. A timeout value of zero is interpreted as an infinite timeout.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440