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

示例

下面的代码示例设置 HttpWebRequest 对象的 Timeout 属性。

// 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)

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

Timeout 是后续使用 GetResponse 方法发出的同步请求等待响应的毫秒数,GetRequestStream 方法等待流。 Timeout 适用于整个请求和响应,不适用于 GetRequestStreamGetResponse 方法调用。 如果未在超时期限内返回资源,则请求将引发 WebException,并将 Status 属性设置为 WebExceptionStatus.Timeout

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

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

谨慎

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

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

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

适用于

另请参阅