WebClient timeout error

你。 提交于 2019-12-12 04:23:58

问题


I created a class like below.

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.

But even after I set timeout to -1 it results in a timeout error.

Is there a solution for this?


回答1:


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:

The value specified is less than zero and is not Infinite.

The default value is 100,000 milliseconds (100 seconds).

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.

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. 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.



来源:https://stackoverflow.com/questions/6262547/webclient-timeout-error

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