Closing an HttpClient Connection Before Logging Out

I am currently using HTTPClient 4 to send a POST request to a remote server as follows:

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

When the server is unavailable, it requires battery life before the connection actually expires. During this runtime, execute () is a blocking call.

What I'm looking for is a way to cancel execute () before a natural timeout so that my thread executing execute () no longer blocks and finishes gracefully.

I tried

 request.abort(); 

and

 httpClient.getConnectionManager().shutdown(); 

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

+6
source share
2 answers

Wrap the call in Future and call receive with a timeout .

0
source

I think you can set connectionTimeout for any maximum time you want to use with these methods: setConnectionTimeout()

 setSoTimeout() 
0
source

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


All Articles