问题
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:
- Never reach the callback function -> timeout callback function
- Reaches and answers successfully
- Reaches and raises an exception
- 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?
回答1:
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
回答2:
.Timeout = time spent trying to establish a connection (not including lookup time) .ReadWriteTimeout = time spent trying to read or write data after connection established
来源:https://stackoverflow.com/questions/387247/i-need-help-setting-net-httpwebrequest-timeout