HttpWebRequest.Timeout 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 GetResponse() 和 GetRequestStream() 方法的超时值(以毫秒为单位)。
public:
virtual property int Timeout { int get(); void set(int value); };
public override int Timeout { get; set; }
member this.Timeout : int with get, set
Public Overrides Property Timeout As Integer
属性值
请求超时前要等待的毫秒数。默认值为 100,000 毫秒 (100 秒) 。
例外
指定的值是小于零,且不是 Infinite。
示例
下面的代码示例设置 Timeout 对象的 属性 HttpWebRequest 。
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest^ myHttpWebRequest = (HttpWebRequest^)( WebRequest::Create( "http://www.contoso.com" ) );
Console::WriteLine( "\nThe timeout time of the request before setting the property is {0} milliseconds.", myHttpWebRequest->Timeout );
// Set the 'Timeout' property of the HttpWebRequest to 10 milliseconds.
myHttpWebRequest->Timeout = 10;
// Display the 'Timeout' property of the 'HttpWebRequest' on the console.
Console::WriteLine( "\nThe timeout time of the request after setting the timeout is {0} milliseconds.", myHttpWebRequest->Timeout );
// A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it
HttpWebResponse^ myHttpWebResponse = (HttpWebResponse^)( myHttpWebRequest->GetResponse() );
// Create a new 'HttpWebRequest' Object to the mentioned URL.
HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
Console.WriteLine("\nThe timeout time of the request before setting the property is {0} milliSeconds.",myHttpWebRequest.Timeout);
// Set the 'Timeout' property of the HttpWebRequest to 10 milliseconds.
myHttpWebRequest.Timeout=10;
// Display the 'Timeout' property of the 'HttpWebRequest' on the console.
Console.WriteLine("\nThe timeout time of the request after setting the timeout is {0} milliSeconds.",myHttpWebRequest.Timeout);
// A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
' Create a new 'HttpWebRequest' Object to the mentioned URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
Console.WriteLine(ControlChars.Cr + "The timeout time of the request before setting the property is {0} milliSeconds", myHttpWebRequest.Timeout)
' Set the 'Timeout' property of the HttpWebRequest to 10 milliseconds.
myHttpWebRequest.Timeout = 10
' Display the 'Timeout' property of the 'HttpWebRequest' on the console.
Console.WriteLine(ControlChars.Cr + "The timeout time of the request after setting the timeout is {0} milliSeconds", myHttpWebRequest.Timeout)
' A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
注解
Timeout 是使用 GetResponse 方法发出的后续同步请求等待响应和 GetRequestStream 方法等待流的毫秒数。 适用于 Timeout 整个请求和响应,而不是单独应用于 GetRequestStream 和 GetResponse 方法调用。 如果在超时期限内未返回资源,则请求将引发 , WebException 并将 Status 属性设置为 WebExceptionStatus.Timeout。
Timeout 属性必须在调用 GetRequestStream或 GetResponse 方法之前设置。 调用GetRequestStream 或 GetResponse 方法后更改 Timeout属性不起作用
属性 Timeout 对使用 BeginGetResponse 或 BeginGetRequestStream 方法发出的异步请求没有影响。
注意
对于异步请求,客户端应用程序实现其自己的超时机制。 请参阅 方法中的 BeginGetResponse 示例。
若要指定读取或写入操作超时前等待的时间量,请使用 ReadWriteTimeout 属性。
域名系统 (DNS) 查询最多可能需要 15 秒才能返回或超时。如果请求包含需要解析的主机名,并且你设置为 Timeout 小于 15 秒的值,则可能需要 15 秒或更多时间才能 WebException 引发 以指示请求超时。
适用于
另请参阅
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈