Monday, March 7, 2011

I need help setting .NET HttpWebRequest timeout

My objective is to get the answer from up to 6000 Urls in the shortest time. It was working very well (12 seconds for 5200 LAN Addresses) until some delay started to happen.

My code uses up to 20 simultaneous HttpWebRequest.BeginGetResponse with ThreadPool.RegisterWaitForSingleObject for timeout handling.

However some (up to 4 in 5,000) of the requests never hit the TimeoutCallback function with the second parameter (timedOut) true, and they waste 5 minutes of my precious time until they hit theBeginGetResponseCallback function and then raise a WebException. The exceptions says something like "the operation reached the time limit", but as the exception message is in portuguese(my native language) I couldn't Google it.

I wonder if I can reduce this time limit to 20 seconds for example. Anyone knows how? I've already tried:

<system.web> <httpRuntime executionTimeout="20"/> </system.web>

But as I'm running it as a Console Application, ASP.NET configurations don't work. And I also tried:

myHttpWebRequest.Timeout = 20*1000;

And

ThreadPool.RegisterWaitForSingleObject(result.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), AsyncState, 20*1000, true);

Without success. Can you help me?

Update What I am trying to say is there are 4 possible results for an asynchronous HTTP request:

1) Never reach the callback function -> timeout callback function 2) Reaches and answers sucessfully 3) Reaches and raises an exception 4) Delay exactly 5 minutes until raise an "time limit" web exception inside the callback function

The 4th possibility is the one is delaying my Application, and I don't know how to shorten that delay

Update Is there a possibility that the method GetResponseStream instead of the GetResponse is who causes the timeout?

From stackoverflow
  • I think that if you set myHttpWebRequest.Timeout becore BeginGetResponse() you should be good with the timeout situation.

    Are you sure that the WebException coming back isn't from the server you're trying to connect to? At this point, I would recommend running Fiddler along with your application and getting a good trace of the HTTP stream going back and forth. That could help you.

    Jader Dias : Thanks Markle, As I said, I've already tryied setting that Timeout property. Someone on the net said that it is valid only for synchronous requests. Fiddler, or a tcpdump, will help me, but I am afraid to search 4 problematic requests in the middle of 5 thousand of good ones.
  • It sounds like you need to set the ReadWriteTimeout property of the request object.

    http://blogs.msdn.com/buckh/archive/2005/02/01/365127.aspx

    Jader Dias : Thanks, perhaps that's the solution. But as I'll have to wait untill the problem occurs again to test it.

0 comments:

Post a Comment