WebRequest.Timeout 属性

定义

获取或设置请求超时之前的时间长度(以毫秒为单位)。

public:
 virtual property int Timeout { int get(); void set(int value); };
public virtual int Timeout { get; set; }
member this.Timeout : int with get, set
Public Overridable Property Timeout As Integer

属性值

请求超时前的时间长度(以毫秒为单位),或指示请求未超时的值 Infinite 。默认值由后代类定义。

例外

当未在子类中重写该属性时,试图获取或设置该属性。

示例

以下示例将 Timeout 属性设置为 10000 毫秒。 如果超时期限在可以返回资源之前过期, WebException 则会引发 。

// Create a new WebRequest Object to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );
Console::WriteLine( "\nThe Timeout time of the request before setting is : {0} milliseconds", myWebRequest->Timeout );

// Set the 'Timeout' property in Milliseconds.
myWebRequest->Timeout = 10000;

// This request will throw a WebException if it reaches the timeout limit
// before it is able to fetch the resource.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

         // Create a new WebRequest Object to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");
Console.WriteLine("\nThe Timeout time of the request before setting is : {0} milliseconds",myWebRequest.Timeout);

// Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout=10000;

// This request will throw a WebException if it reaches the timeout limit before it is able to fetch the resource.
         WebResponse myWebResponse=myWebRequest.GetResponse();

   ' Create a new WebRequest Object to the mentioned URL.
   Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
   Console.WriteLine(ControlChars.Cr + "The Timeout time of the request before setting is : {0} milliseconds", myWebRequest.Timeout)

   ' Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout = 10000

  ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
   Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

注解

属性 Timeout 指示请求超时并引发 WebException的时间长度(以毫秒为单位)。 属性 Timeout 仅影响使用 GetResponse 方法发出的同步请求。 若要超时异步请求,请使用 Abort 方法。

备注

WebRequest 是类 abstract 。 实例在运行时的实际行为 WebRequest 由 方法返回 WebRequest.Create 的后代类确定。 有关默认值和异常的详细信息,请参阅后代类(如 HttpWebRequestFileWebRequest)的文档。

继承者说明

后代类通过将字段设置为 TimeoutWebExceptionStatus引发 来发出超时信号。 当 Timeout 设置为 Infinite 时,后代类不会超时。

适用于

另请参阅