HttpWebRequest.Timeout 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 和 GetRequestStream() 方法的逾時值(毫秒GetResponse())。
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.
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)
備註
謹慎
WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。
Timeout 是後續同步請求與該 GetResponse 方法 GetRequestStream 相同的毫秒數,等待回應,而方法等待串流的時間。 這 Timeout 適用於整個請求和回應,而不是單獨針對 GetRequestStream and GetResponse 方法呼叫。 若資源未在逾時期內回傳,請求會拋 WebException 出一個屬性設定 Status 為 WebExceptionStatus.Timeout的 。
Timeout必須先設定該屬性,才能呼叫 GetRequestStream or GetResponse 方法。 呼叫 GetRequestStream or GetResponse 方法後更改Timeout屬性則沒有影響
此 Timeout 特性對使用 BeginGetResponse or BeginGetRequestStream 方法發出的非同步請求不影響。
謹慎
在異步要求的情況下,用戶端應用程式會實作自己的逾時機制。 請參考該 BeginGetResponse 方法中的範例。
若要指定讀取或寫入操作逾時前的等待時間,請使用屬性 ReadWriteTimeout 。
網域名稱系統(DNS)查詢可能需長達15秒才能返回或逾時。如果你的請求包含需要解決的主機名稱,且你設定 Timeout 的值少於 15 秒,可能需要 15 秒或更久才會拋出 a WebException 表示請求暫停。