HttpWebRequest.Timeout 属性

定义

获取或设置 GetResponse()GetRequestStream() 方法的超时值(以毫秒为单位)。Gets or sets the time-out value in milliseconds for the GetResponse() and GetRequestStream() methods.

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

属性值

请求超时前等待的毫秒数。默认值为100000毫秒(100秒)。The number of milliseconds to wait before the request times out. The default value is 100,000 milliseconds (100 seconds).

异常

指定的值是小于零,且不是 InfiniteThe value specified is less than zero and is not Infinite.

示例

下面的代码示例设置 HttpWebRequest 对象的 Timeout 属性。The following code example sets the Timeout property of the HttpWebRequest object.

// 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 is the number of milliseconds that a subsequent synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. Timeout 适用于整个请求和响应,而不是单独到 GetRequestStreamGetResponse 方法调用。The Timeout applies to the entire request and response, not individually to the GetRequestStream and GetResponse method calls. 如果未在超时期限内返回资源,则请求会引发 WebException,并将 Status 属性设置为 "WebExceptionStatus.Timeout"。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 方法之前设置。The Timeout property must be set before the GetRequestStream or GetResponse method is called. 调用GetRequestStreamGetResponse 方法后更改 Timeout属性不起作用Changing the Timeout property after calling the GetRequestStream or GetResponse method has no effect

Timeout 属性对通过 BeginGetResponseBeginGetRequestStream 方法发出的异步请求不起作用。The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream method.

注意

对于异步请求,客户端应用程序实现其自己的超时机制。In the case of asynchronous requests, the client application implements its own time-out mechanism. 请参阅 BeginGetResponse 方法中的示例。Refer to the example in the BeginGetResponse method.

若要指定在读或写操作超时之前要等待的时间量,请使用 ReadWriteTimeout 属性。To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.

域名系统(DNS)查询可能需要长达15秒钟的时间来返回或超时。如果你的请求中包含需要解析的主机名,并且你将 Timeout 设置为小于15秒的值,则可能需要15秒或更长时间,然后才会引发 WebException 来指示请求超时。A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

适用于

另请参阅