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

範例

下列程式碼範例會 Timeout 設定 物件的 屬性 HttpWebRequest

// 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會套用至整個要求和回應,而不是個別套用至 GetRequestStreamGetResponse 方法呼叫。 如果資源未在逾時期間內傳回,要求會 WebException 擲回 屬性設定為 WebExceptionStatus.TimeoutStatus

必須先 Timeout 設定 屬性, GetRequestStream 才能呼叫 或 GetResponse 方法。 呼叫 GetRequestStreamGetResponse 方法之後變更 Timeout 屬性沒有任何作用

屬性 Timeout 不會影響使用 BeginGetResponseBeginGetRequestStream 方法提出的非同步要求。

警告

在非同步要求的情況下,用戶端應用程式會實作自己的逾時機制。 請參閱 方法中的 BeginGetResponse 範例。

若要指定讀取或寫入作業逾時之前要等候的時間量,請使用 ReadWriteTimeout 屬性。

網域名稱系統 (DNS) 查詢最多可能需要 15 秒才能傳回或逾時。如果您的要求包含需要解析的主機名稱,而且您設定 Timeout 為小於 15 秒的值,則擲回 之前 WebException 可能需要 15 秒以上,以指出要求的逾時。

適用於

另請參閱