WebClient timeout error
Asked Answered
C

1

0

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?

Copilot answered 7/6, 2011 at 8:29 Comment(0)
H
3

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.

Hardandfast answered 7/6, 2011 at 8:37 Comment(1)
In case anyone is wondering why they can't find ReadWriteTimeout property, that's because ReadWriteTimeout is a property of HttpWebRequest, so you probably have to cast toHttpWebRequest instead of WebRequest.Kashgar

© 2022 - 2024 — McMap. All rights reserved.