简体   繁体   中英

WebClient timeout error

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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