How to make HTTP connection timeout / disconnected after a period of time?

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

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 does he wait more than one minute without the hope of a timeout / shutdown? Where could the problem be?

+3
source share
3 answers

There are 2 timeouts in HTTPClient, try setting both parameters

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

However, the values ​​will be ignored if the connection is stuck in calling its own socket. Therefore, you may need to run the request in a different thread so that you can disable it. See my answer to this question on how to do this,

java -

+4

SO_TIMEOUT?

- (SO_TIMEOUT) , , . - -.

0

Source: https://habr.com/ru/post/1726009/


All Articles