[英]c# HttpWebRequest Timeout setting to zero
According to MSDN, default WebRequest timeout is 100 seconds (100,000 ms). 根据MSDN,默认WebRequest超时为100秒(100,000毫秒)。 Will that mean that setting Timeout to 0 (zero) will timeout request immediatelly? 这是否意味着将Timeout设置为0(零)会立即超时请求?
If so, when would you want to do something like that? 如果是这样,你什么时候想做那样的事?
Yes, it will timout immediately, you can test it yourself easily: 是的,它会立即提示,您可以轻松自己测试:
try
{
WebRequest myWebRequest = WebRequest.Create("http://stackoverflow.com/questions/38340099/c-sharp-httpwebrequest-timeout-setting-to-zero");
myWebRequest.Timeout = 0;
WebResponse myWebResponse = myWebRequest.GetResponse();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); // timeout exceeded
}
Why? 为什么? I've asked myself the same. 我问自己一样。 Maybe for testing purposes or if you need a default value that clearly doesn't work accidentially to ensure that the property will be set in any case. 可能出于测试目的,或者如果您需要一个明显无法正常工作的默认值,以确保在任何情况下都可以设置该属性。
Interestingly negative values aren't allowed apart from -1, since that is the value of System.Threading.Timeout.Infinite
. 有趣的是,除了-1之外不允许使用负值,因为这是System.Threading.Timeout.Infinite
的值。 Here's the source: 这是来源:
public override int Timeout {
get {
return _Timeout;
}
set {
if (value<0 && value!=System.Threading.Timeout.Infinite) {
throw new ArgumentOutOfRangeException("value", SR.GetString(SR.net_io_timeout_use_ge_zero));
}
if (_Timeout != value)
{
_Timeout = value;
_TimerQueue = null;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.