HttpWebRequest.Timeout 屬性

定義

取得或設定 GetResponse()GetRequestStream() 方法的逾時值 (以毫秒為單位)。Gets or sets the time-out value in milliseconds for the GetResponse() and GetRequestStream() methods.

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

屬性值

Int32

要求超時前等待的毫秒數。預設值為100000毫秒 (100 秒) 。The number of milliseconds to wait before the request times out. The default value is 100,000 milliseconds (100 seconds).

例外狀況

指定的值小於零且不是 InfiniteThe value specified is less than zero and is not Infinite.

範例

下列程式碼範例會設定 Timeout 物件的屬性 HttpWebRequestThe following code example sets the Timeout property of the HttpWebRequest object.

// 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 is the number of milliseconds that a subsequent synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. Timeout會套用至整個要求和回應,而不是個別用於 GetRequestStreamGetResponse 方法呼叫。The Timeout applies to the entire request and response, not individually to the GetRequestStream and GetResponse method calls. 如果資源不會在超時期限內傳回,則要求會擲回, WebException 並將 Status 屬性設定為 WebExceptionStatus.TimeoutIf the resource is not returned within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus.Timeout.

Timeout呼叫或方法之前,必須先設定屬性 GetRequestStream GetResponseThe Timeout property must be set before the GetRequestStream or GetResponse method is called. Timeout呼叫或方法之後變更屬性 GetRequestStream GetResponse 不會有任何作用Changing the Timeout property after calling the GetRequestStream or GetResponse method has no effect

Timeout屬性不會影響使用或方法所做的非同步 BeginGetResponse 要求 BeginGetRequestStreamThe Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream method.

警告

在非同步要求的案例中,用戶端應用程式會執行自己的超時機制。In the case of asynchronous requests, the client application implements its own time-out mechanism. 請參閱方法中的範例 BeginGetResponseRefer to the example in the BeginGetResponse method.

若要指定讀取或寫入作業超時之前要等待的時間長度,請使用 ReadWriteTimeout 屬性。To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.

網域名稱系統 (DNS) 查詢可能需要15秒的時間才會傳回或超時。如果您的要求包含需要解析的主機名稱,而且您將設定 Timeout 為小於15秒的值,則可能需要15秒以上的時間,才會擲回, WebException 以指出要求的超時時間。A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

適用於

另請參閱