共用方式為


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

範例

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

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

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

Timeout 是後續使用 GetResponse 方法提出的同步要求等候回應的毫秒數,而 GetRequestStream 方法會等候數據流。 Timeout 適用於整個要求和回應,而不是個別套用至 GetRequestStreamGetResponse 方法呼叫。 如果資源未在逾時期間內傳回,要求會擲回 WebException,並將 Status 屬性設定為 WebExceptionStatus.Timeout

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

Timeout 屬性對使用 BeginGetResponseBeginGetRequestStream 方法進行的異步要求沒有任何作用。

謹慎

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

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

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

適用於

另請參閱