HttpWebRequest.Timeout Eigenschaft

Definition

Ruft den Timeoutwert in Millisekunden für die GetResponse()-Methode und die GetRequestStream()-Methode ab oder legt diesen fest.

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 von Millisekunden, die gewartet werden müssen, bis die Anforderung ein Zeitüberschreitung aufweist. Der Standardwert ist 100.000 Millisekunden (100 Sekunden).

Ausnahmen

Der angegebene Wert ist kleiner als null (0) und ungleich 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

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 Stream. Gilt Timeout für die gesamte Anforderung und Antwort, nicht einzeln für die GetRequestStream Methodenaufrufe und GetResponse . Wenn die Ressource nicht innerhalb des Timeoutzeitraums zurückgegeben wird, löst die Anforderung ein aus WebException , wobei die Status -Eigenschaft auf WebExceptionStatus.Timeoutfestgelegt ist.

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

Die Timeout -Eigenschaft hat keine Auswirkungen auf asynchrone Anforderungen, die mit der BeginGetResponse -Methode oder BeginGetRequestStream vorgenommen werden.

Achtung

Bei asynchronen Anforderungen implementiert die Clientanwendung einen eigenen Timeoutmechanismus. Sehen Sie sich das Beispiel in der -Methode an BeginGetResponse .

Verwenden ReadWriteTimeout Sie die -Eigenschaft, um anzugeben, wie lange gewartet werden muss, bis ein Lese- oder Schreibvorgang ein Timeout auftritt.

Eine DNS-Abfrage (Domain Name System) kann bis zu 15 Sekunden dauern, bis sie zurückgegeben wird oder ein Timeout erfolgt. Wenn Ihre Anforderung einen Hostnamen enthält, der eine Auflösung erfordert und Sie auf einen Wert von weniger als 15 Sekunden festgelegt Timeout haben, kann es 15 Sekunden oder länger dauern, bis ein WebException ausgelöst wird, um ein Timeout für Ihre Anforderung anzugeben.

Gilt für:

Weitere Informationen