HttpWebRequest.Timeout 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 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會套用至整個要求和回應,而不是個別套用至 GetRequestStream 和 GetResponse 方法呼叫。 如果資源未在逾時期間內傳回,要求會WebException擲回 屬性設定為 WebExceptionStatus.Timeout的 Status 。
必須先 Timeout 設定 屬性, GetRequestStream 才能呼叫 或 GetResponse 方法。 呼叫 GetRequestStream 或 GetResponse 方法之後變更 Timeout 屬性沒有任何作用
屬性 Timeout 不會影響使用 BeginGetResponse 或 BeginGetRequestStream 方法提出的異步要求。
警告
在異步要求的情況下,用戶端應用程式會實作自己的逾時機制。 請參閱 方法中的 BeginGetResponse 範例。
若要指定讀取或寫入作業逾時之前要等候的時間量,請使用 ReadWriteTimeout 屬性。
功能變數名稱系統 (DNS) 查詢最多可能需要 15 秒才能傳回或逾時。如果您的要求包含需要解析的主機名,而且您設定 Timeout 為小於 15 秒的值,則擲回 之前 WebException 可能需要 15 秒以上,以指出要求的逾時。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應