Question

Are there any known issues with canceling HttpWebRequest HTTP requests? We find that when we cancel 4-5 requests, the next request hangs indefinitely.

If there are no known problems with this, then I'm probably doing something wrong... where is a good resource example that shows how this works (a complete solution, not a couple of code snippets)?

If there are known problems, what can I do to work around them, in order to effectively cancel as many requests as I need to?

Was it helpful?

Solution

After you cancel the request, take care to close the response stream, otherwise you will have leaks.

I usually use "using" when obtain the response from the web request to ensure that the response is closed:

WebRequest request = WebRequest.Create("http://google.com");

using (WebResponse response = request.GetResponse())
{
   //do my job
}

That way, even if you cancel the request, or it throws an exception during response reading, the resposne and it's stream will be closed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top