WebRequest.Timeout 屬性

定義

取得或設定要求逾時之前的時間長度 (以毫秒為單位)。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

屬性值

Int32

要求超時之前的時間長度(以毫秒為單位),或 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()

備註

Timeout屬性以毫秒為單位表示要求超時並擲回的時間長度(以毫秒為單位) WebExceptionThe Timeout property indicates the length of time, in milliseconds, until the request times out and throws a WebException. Timeout屬性只會影響使用方法所進行的同步要求 GetResponseThe Timeout property affects only synchronous requests made with the GetResponse method. 若要時間非同步要求,請使用 Abort 方法。To time out asynchronous requests, use the Abort method.

注意

WebRequest類別是 abstract 類別。The WebRequest class is an abstract class. WebRequest實例在執行時間的實際行為是由方法所傳回的子代類別所決定 WebRequest.CreateThe actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 如需預設值和例外狀況的詳細資訊,請參閱下階類別的檔,例如 HttpWebRequestFileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

給繼承者的注意事項

子系類別會擲回 WebException ,並將 Status 欄位設定為,以發出超時信號 TimeoutDescendant 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.

適用於

另請參閱