简体   繁体   English

WebClient超时解决方案不起作用(通过子类设置超时)

[英]WebClient Timeout solution doesn't work (subclassing to set Timeout)

I am trying to use this as a commonly accepted solution for setting a timeouts for WeblClient calls. 我试图将其用作设置WeblClient呼叫超时的普遍接受的解决方案。 In my test case of requesting a url from an offline machine: I consistently get 20 second timeouts when set at 1 second. 在从脱机计算机请求url的测试案例中:设置为1秒时,我始终会遇到20秒超时。

public class TimeoutWebClient : WebClient
{
    public TimeoutWebClient(TimeSpan timeout)
    {
        _timeout = timeout;
    }
    private TimeSpan _timeout;

   protected override WebRequest GetWebRequest(Uri address)
   {
       WebRequest request = base.GetWebRequest(address);
       request.Timeout = (int)_timeout.TotalMilliseconds;
       return request;
   }
}

Server based timeouts can't matter in this scenario. 在这种情况下,基于服务器的超时无关紧要。 What could I be missing? 我可能会缺少什么?

I did find a snippet that set both HttpWebRequest.ReadWriteTimeout and HttpWebRequest.ServicePoint.MaxIdleTime . 我确实找到了同时设置HttpWebRequest.ReadWriteTimeoutHttpWebRequest.ServicePoint.MaxIdleTime的代码段。 But setting these to the timeout value still didn't make a difference and am still getting ~20 second timeouts. 但是将这些设置为超时值仍然没有任何区别,并且仍然会导致〜20秒的超时。

Any other thoughts of what could cause this behavior? 关于什么可能导致此行为的其他想法?

Any chance you are using async download method? 您是否有使用异步下载方法的机会?

From the MSDN: 从MSDN:

The Timeout property affects only synchronous requests made with the GetResponse method. Timeout属性仅影响使用GetResponse方法发出的同步请求。 To time out asynchronous requests, use the Abort method. 要使异步请求超时,请使用Abort方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM