问题
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