WebRequest.Timeout WebRequest.Timeout WebRequest.Timeout WebRequest.Timeout Property

定义

获取或设置请求超时之前的时间长度(以毫秒为单位)。Gets or sets the length of time, in milliseconds, before the request times out.

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 值。默认值是由子代类定义的。The length of time, in milliseconds, until the request times out, or the value Infinite to indicate that the request does not time out. The default value is defined by the descendant class.

异常

当未在子类中重写该属性时,试图获取或设置该属性。Any attempt is made to get or set the property, when the property is not overridden in a descendant class.

示例

下面的示例将Timeout属性设置为10000毫秒。The following example sets the Timeout property to 10000 milliseconds. 如果超时期限在可以返回资源之前过期, WebException则会引发。If the timeout period expires before the resource can be returned, a WebException is thrown.

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

注解

属性指示请求超时前的时间长度(以毫秒为单位),并WebException引发。 TimeoutThe Timeout property indicates the length of time, in milliseconds, until the request times out and throws a WebException. Timeout属性仅影响GetResponse通过方法发出的同步请求。The Timeout property affects only synchronous requests made with the GetResponse method. 若要超时异步请求,请使用Abort方法。To time out asynchronous requests, use the Abort method.

备注

类是一个abstract类。 WebRequestThe WebRequest class is an abstract class. WebRequest实例在运行时的实际行为由WebRequest.Create方法返回的子代类确定。The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 有关默认值和异常的详细信息,请参阅子代类的文档,例如HttpWebRequest和。 FileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

继承者说明

子类通过引发WebException Status ,并将字段设置为, Timeout以指示超时。Descendant classes signal a timeout by throwing a WebException with the Status field set to Timeout. Timeout设置为Infinite子类时,不会超时。When Timeout is set to Infinite the descendant class does not time out.

适用于

另请参阅