I need help setting .NET HttpWebRequest timeout

戏子无情 提交于 2019-12-23 18:00:25

问题


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 successfully
  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?


回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!