HttpWebRequest.Timeout Właściwość

Definicja

Pobiera lub ustawia wartość limitu czasu w milisekundach dla GetResponse() metod i 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

Wartość właściwości

Liczba milisekund oczekiwania przed upływem limitu czasu żądania. Wartość domyślna to 100 000 milisekund (100 sekund).

Wyjątki

Określona wartość jest mniejsza niż zero i nie Infinitejest .

Przykłady

Poniższy przykład kodu ustawia Timeout właściwość HttpWebRequest obiektu.

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

Uwagi

Timeout jest liczbą milisekund, którą kolejne synchroniczne żądanie wykonane za GetResponse pomocą metody czeka na odpowiedź, a GetRequestStream metoda czeka na strumień. Element Timeout dotyczy całego żądania i odpowiedzi, a nie indywidualnie do GetRequestStream wywołań metod i GetResponse . Jeśli zasób nie jest zwracany w okresie limitu czasu, żądanie zgłasza WebException wyjątek z właściwością ustawioną Status na WebExceptionStatus.Timeoutwartość .

Właściwość musi zostać ustawiona Timeout przed wywołaną GetRequestStream metodą lub GetResponse . Timeout Zmiana właściwości po wywołaniu GetRequestStream metody or GetResponse nie ma wpływu

Właściwość Timeout nie ma wpływu na żądania asynchroniczne wykonywane za pomocą BeginGetResponse metody or BeginGetRequestStream .

Przestroga

W przypadku żądań asynchronicznych aplikacja kliencka implementuje własny mechanizm limitu czasu. Zapoznaj się z przykładem w metodzie BeginGetResponse .

Aby określić czas oczekiwania przed przekroczeniem limitu czasu operacji odczytu lub zapisu, użyj ReadWriteTimeout właściwości .

Zapytanie systemu nazw domen (DNS) może potrwać do 15 sekund, aby zwrócić lub upłynął limit czasu. Jeśli żądanie zawiera nazwę hosta, która wymaga rozwiązania i ustawiono Timeout wartość mniejszą niż 15 sekund, może upłynąć 15 sekund lub więcej, zanim WebException zostanie zgłoszony wyjątek , aby wskazać limit czasu żądania.

Dotyczy

Zobacz też