Freigeben über


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 soll, bevor das Anforderungs-Zeitüberschreitungsüberschreitung aufgetreten ist. 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 der Millisekunden, die eine nachfolgende synchrone Anforderung mit der GetResponse -Methode auf eine Antwort wartet, und die GetRequestStream -Methode wartet auf einen Datenstrom. 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 einen aus WebException , wobei die Status -Eigenschaft auf WebExceptionStatus.Timeoutfestgelegt ist.

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

Die Timeout -Eigenschaft hat keine Auswirkung auf asynchrone Anforderungen, die mit der - oder BeginGetRequestStream -BeginGetResponseMethode ausgeführt werden.

Achtung

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

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

Bei einer DNS-Abfrage (Domain Name System) kann es bis zu 15 Sekunden dauern, bis sie zurückgegeben oder ein Timeout ausgeführt wird. 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 mehr dauern, bis ein WebException Ausgelöst wird, um ein Timeout für Ihre Anforderung anzuzeigen.

Gilt für:

Weitere Informationen