HttpWebRequest.Timeout 属性

定义

获取或设置 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

属性值

Int32

请求超时之前要等待的毫秒数。默认值为 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 整个请求和响应,而不是单独应用于 GetRequestStreamGetResponse 方法调用。 If the resource is not returned within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus.Timeout.

Timeout 属性必须在调用 GetRequestStreamGetResponse 方法之前设置。 调用GetRequestStreamGetResponse 方法后更改 Timeout属性不起作用

Timeout 属性对使用 BeginGetResponseBeginGetRequestStream 方法发出的异步请求没有任何影响。

注意

对于异步请求,客户端应用程序实现其自己的超时机制。 请参阅方法中的 BeginGetResponse 示例。

若要指定读取或写入操作超时之前等待的时间量,请使用该 ReadWriteTimeout 属性。

域名系统 (DNS) 查询可能需要长达 15 秒才能返回或超时。如果请求包含需要解析的主机名,并且你设置为 Timeout 小于 15 秒的值,则引发请求前 WebException 可能需要 15 秒或更多秒才能指示超时。

适用于

另请参阅