Jason
Jason

Reputation: 17089

HttpWebRequest, known problems with canceling requests?

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?

Upvotes: 0

Views: 1642

Answers (1)

Sunny Milenov
Sunny Milenov

Reputation: 22310

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.

Upvotes: 1

Related Questions