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

属性值

请求超时前等待的毫秒数。默认值为 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 方法调用。 如果在超时期限内未返回资源,则请求将引发 , WebException 并将 Status 属性设置为 WebExceptionStatus.Timeout

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

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

注意

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

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

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

适用于

另请参阅