Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to cancel a thread making a http request from another thread

Any suggestion on how do we kill a thread which has made a http post request and waiting till getting response. I want to kill this thread from another thread running parallely before it could receive any response from request. I got suggestion to close or abort the http post request but did not workout, can anyone suggest how can we abort the http request in this scenario or any other way to achieve this.

like image 782
Muzammil Shareef Avatar asked Oct 17 '22 09:10

Muzammil Shareef


1 Answers

The thread that made the request is blocked while waiting the server to respond. If you take a thread dump and see, it will be in some read() method, but not on a Thread.sleep. So calling interrupt() will not have an effect (unless it's an InterruptibleChannel). Do you have access to the socket or the output stream on which the POST request was made? If so, you can close that, which will bring out of the read wait with an exception. Do you have a code sample that you can put up?

like image 131
jedikasun Avatar answered Oct 21 '22 05:10

jedikasun