Share via


HttpWebRequest.Timeout Properti

Definisi

Mendapatkan atau menetapkan nilai waktu habis dalam milidetik untuk GetResponse() metode dan 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

Nilai Properti

Jumlah milidetik untuk menunggu sebelum waktu permintaan habis. Nilai defaultnya adalah 100.000 milidetik (100 detik).

Pengecualian

Nilai yang ditentukan kurang dari nol dan bukan Infinite.

Contoh

Contoh kode berikut mengatur Timeout properti HttpWebRequest objek .

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

Keterangan

Timeout adalah jumlah milidetik bahwa permintaan sinkron berikutnya yang dibuat dengan GetResponse metode menunggu respons, dan GetRequestStream metode menunggu aliran. berlaku Timeout untuk seluruh permintaan dan respons, tidak secara individual untuk GetRequestStream panggilan metode dan GetResponse . Jika sumber daya tidak dikembalikan dalam periode waktu habis, permintaan akan melempar dengan properti yang WebExceptionStatus diatur ke WebExceptionStatus.Timeout.

Properti Timeout harus diatur sebelum GetRequestStream metode atau GetResponse dipanggil. Mengubah Timeout properti setelah memanggil GetRequestStream metode atau GetResponse tidak berpengaruh

Properti Timeout tidak berpengaruh pada permintaan asinkron yang dibuat dengan BeginGetResponse metode atau BeginGetRequestStream .

Perhatian

Dalam kasus permintaan asinkron, aplikasi klien menerapkan mekanisme waktu habisnya sendiri. Lihat contoh dalam BeginGetResponse metode .

Untuk menentukan jumlah waktu tunggu sebelum waktu operasi baca atau tulis habis, gunakan ReadWriteTimeout properti .

Kueri Sistem Nama Domain (DNS) mungkin membutuhkan waktu hingga 15 detik untuk kembali atau kehabisan waktu. Jika permintaan Anda berisi nama host yang memerlukan resolusi dan Anda mengatur Timeout ke nilai kurang dari 15 detik, mungkin perlu waktu 15 detik atau lebih sebelum WebException dilemparkan untuk menunjukkan batas waktu pada permintaan Anda.

Berlaku untuk

Lihat juga