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 秒) です。

例外

指定された値が 0 未満で、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要求と応答全体に適用され、 メソッド呼び出しと GetResponse メソッド呼び出しには個別にGetRequestStream適用されません。 タイムアウト期間内にリソースが返されない場合、要求は プロパティが にWebExceptionStatus.Timeout設定された StatusWebExceptionスローします。

または GetResponse メソッドをTimeout呼び出す前に、 プロパティをGetRequestStream設定する必要があります。 メソッドまたは GetResponse メソッドのTimeout呼び出し後に プロパティをGetRequestStream変更しても効果はありません

プロパティはTimeout、 メソッドまたは BeginGetRequestStream メソッドで行われた非同期要求にはBeginGetResponse影響しません。

注意事項

非同期要求の場合、クライアント アプリケーションは独自のタイムアウト メカニズムを実装します。 メソッドの例を BeginGetResponse 参照してください。

読み取りまたは書き込み操作がタイムアウトするまでの待機時間を指定するには、 プロパティを ReadWriteTimeout 使用します。

ドメイン ネーム システム (DNS) クエリが返されるかタイムアウトするまでに最大 15 秒かかる場合があります。要求に解決が必要なホスト名が含まれており、15 秒未満の値に設定 Timeout した場合は、 がスローされて WebException 要求のタイムアウトが示されるまでに 15 秒以上かかることがあります。

適用対象

こちらもご覧ください