简体   繁体   English

WebClient 超时错误

[英]WebClient timeout error

I created a class like below.我创建了一个 class,如下所示。

public class WebDownload : WebClient
{
    private int _timeout;
    /// <summary>
    /// Time in milliseconds
    /// </summary>
    public int Timeout
    {
        get
        {
            return _timeout;
        }
        set
        {
            _timeout = value;
        }
    }

    public WebDownload()
    {
        this._timeout = -1;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {
        WebRequest result = base.GetWebRequest(address);
        result.Timeout = this._timeout;
        return result;
    }
}

When I create an object of this class it creates a webclient object and sets timeout to -1 so that it waits unlimited time for a response.当我创建这个 class 的 object 时,它会创建一个 Web 客户端 object 并将timeout设置为 -1,以便它无限等待响应。

But even after I set timeout to -1 it results in a timeout error.但即使在我将timeout设置为 -1 后,它也会导致超时错误。

Is there a solution for this?有解决方案吗?

I have ho idea where you got the -1 part from, but in the MSDN article regarding Timeout it says that it will throw an ArgumentOutOfRangeException if:我知道你从哪里得到-1部分,但是在关于Timeout的 MSDN 文章中,它说如果出现以下情况,它将抛出ArgumentOutOfRangeException

The value specified is less than zero and is not Infinite.指定的值小于零并且不是无限的。

The default value is 100,000 milliseconds (100 seconds).默认值为100,000 毫秒(100 秒)。

One more thing to take into account:还有一件事要考虑:

To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.若要指定在读取或写入操作超时之前等待的时间量,请使用ReadWriteTimeout属性。

A Domain Name System (DNS) query may take up to 15 seconds to return or time out.域名系统 (DNS) 查询最多可能需要15 秒才能返回或超时。 If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.如果您的请求包含需要解析的主机名,并且您将 Timeout 设置为小于 15 秒的值,则可能需要 15 秒或更长时间才能引发 WebException 以指示您的请求超时。

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

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