Teilen über


HttpWebRequest.Timeout Eigenschaft

Definition

Dient zum Abrufen oder Festlegen des Timeoutwerts in Millisekunden für die methoden GetResponse() und 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

Eigenschaftswert

Die Anzahl der Millisekunden, die gewartet werden sollen, bevor das Anforderungs-Zeitüberschreitungsüberschreitung erfolgt. Der Standardwert ist 100.000 Millisekunden (100 Sekunden).

Ausnahmen

Der angegebene Wert ist kleiner als Null und wird nicht Infinite.

Beispiele

Im folgenden Codebeispiel wird die Timeout-Eigenschaft des HttpWebRequest-Objekts festgelegt.

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

Hinweise

Vorsicht

WebRequest, HttpWebRequest, ServicePointund WebClient sind veraltet, und Sie sollten sie nicht für die neue Entwicklung verwenden. Verwenden Sie stattdessen HttpClient.

Timeout ist die Anzahl von Millisekunden, die eine nachfolgende synchrone Anforderung mit der GetResponse-Methode auf eine Antwort wartet, und die GetRequestStream-Methode wartet auf einen Datenstrom. Die Timeout gilt für die gesamte Anforderung und Antwort, nicht einzeln für die GetRequestStream und GetResponse Methodenaufrufe. Wenn die Ressource nicht innerhalb des Timeoutzeitraums zurückgegeben wird, löst die Anforderung eine WebException aus, wobei die Status-Eigenschaft auf WebExceptionStatus.Timeoutfestgelegt ist.

Die Timeout-Eigenschaft muss festgelegt werden, bevor die methode GetRequestStream oder GetResponse aufgerufen wird. Das Ändern der Timeout-Eigenschaft nach dem Aufrufen der GetRequestStream- oder GetResponse-Methode hat keine Auswirkung.

Die eigenschaft Timeout hat keine Auswirkungen auf asynchrone Anforderungen, die mit der BeginGetResponse- oder BeginGetRequestStream methode vorgenommen wurden.

Vorsicht

Bei asynchronen Anforderungen implementiert die Clientanwendung einen eigenen Timeoutmechanismus. Verweisen Sie auf das Beispiel in der BeginGetResponse-Methode.

Verwenden Sie die ReadWriteTimeout-Eigenschaft, um anzugeben, wie lange gewartet werden soll, bevor ein Lese- oder Schreibvorgang ausläuft.

Eine DNS-Abfrage (Domain Name System) kann bis zu 15 Sekunden dauern, bis ein Timeout zurückgegeben oder beendet wird. Wenn Ihre Anforderung einen Hostnamen enthält, der eine Auflösung erfordert und Sie Timeout auf einen Wert unter 15 Sekunden festlegen, kann es 15 Sekunden oder mehr dauern, bis ein WebException ausgelöst wird, um ein Timeout für Ihre Anforderung anzugeben.

Gilt für:

Weitere Informationen