HttpWebRequest.Timeout Proprietà

Definizione

Ottiene o imposta il valore di timeout in millisecondi per i metodi GetResponse() e 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

Valore della proprietà

Numero di millisecondi di attesa prima del timeout della richiesta. Il valore predefinito è 100.000 millisecondi (100 secondi).

Eccezioni

Il valore specificato è minore di zero e non è Infinite.

Esempio

Nell'esempio di codice seguente viene impostata la Timeout proprietà dell'oggetto HttpWebRequest .

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

Commenti

Timeout è il numero di millisecondi che una richiesta sincrona successiva effettuata con il GetResponse metodo attende una risposta e il GetRequestStream metodo attende un flusso. Si Timeout applica all'intera richiesta e alla risposta, non singolarmente alle chiamate al GetRequestStream metodo e GetResponse . Se la risorsa non viene restituita entro il periodo di timeout, la richiesta genera un WebException oggetto con la Status proprietà impostata su WebExceptionStatus.Timeout.

La Timeout proprietà deve essere impostata prima che venga chiamato il GetRequestStream metodo o GetResponse . La modifica della proprietà dopo la Timeout chiamata al GetRequestStream metodo o GetResponse non ha alcun effetto

La Timeout proprietà non ha alcun effetto sulle richieste asincrone effettuate con il BeginGetResponse metodo o BeginGetRequestStream .

Attenzione

Nel caso di richieste asincrone, l'applicazione client implementa il proprio meccanismo di timeout. Fare riferimento all'esempio nel BeginGetResponse metodo .

Per specificare la quantità di tempo di attesa prima del timeout di un'operazione di lettura o scrittura, utilizzare la ReadWriteTimeout proprietà .

La restituzione o il timeout di una query DNS (Domain Name System) può richiedere fino a 15 secondi. Se la richiesta contiene un nome host che richiede la risoluzione e si imposta Timeout su un valore inferiore a 15 secondi, potrebbero essere necessari 15 secondi o più prima che venga generata un'eccezione WebException per indicare un timeout nella richiesta.

Si applica a

Vedi anche