Moritz
Moritz

Reputation: 10352

Closing HttpClient connection before timout is reached

I am currently using the HTTPClient 4 to make a POST request to a remote server like this:

  HttpResponse response = httpClient.execute( request );
  InputStream is = response.getEntity().getContent();

When the server is not reachable it takes a self-configured amount of time before the connection actually times out. During that periode the execute() is a blocking call.

What i am looking for is a way to cancel the execute() before the natural timeout so that my thread running the execute() is not blocked anymore and will finish gracefully.

I have tried

request.abort();

and

httpClient.getConnectionManager().shutdown();

But both of these calls do not interrupt the execute(). Is there any other way to cancel the ongoing connection attempt?

Upvotes: 6

Views: 1684

Answers (2)

kosa
kosa

Reputation: 66677

I think you can set connectionTimeout for whatever the max time you want using these methods:setConnectionTimeout()

setSoTimeout()

Upvotes: 0

alphazero
alphazero

Reputation: 27244

Wrap the call in a Future and invoke get with timeout.

Upvotes: 1

Related Questions