次の方法で共有


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されていません。

次のコード例では、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 は、GetRequestStream および GetResponse メソッド呼び出しに個別に適用されるのではなく、要求と応答全体に適用されます。 タイムアウト期間内にリソースが返されない場合、要求は、Status プロパティが WebExceptionStatus.Timeoutに設定された WebException をスローします。

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

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

注意

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

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

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

適用対象

こちらもご覧ください