HttpWebRequest.Timeout Свойство

Определение

Возвращает или задает значение времени ожидания в миллисекундах для методов GetResponse() и 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

Значение свойства

Время ожидания в миллисекундах до истечения времени ожидания запроса. Значение по умолчанию — 100 000 миллисекунд (100 секунд).

Исключения

Указанное значение меньше нуля и не равно Infinite.

Примеры

В следующем примере кода задается Timeout свойство 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)

Комментарии

Timeout — это количество миллисекунд, в течение которых последующий синхронный запрос, выполненный с GetResponse помощью метода, ожидает ответа, а GetRequestStream метод ожидает потока. Применяется Timeout ко всему запросу и ответу, а не по отдельности к вызовам GetRequestStream метода и GetResponse . Если ресурс не возвращается в течение периода ожидания, запрос вызывает исключение со свойством WebExceptionStatus , равным WebExceptionStatus.Timeout.

Свойство Timeout должно быть задано перед вызовом GetRequestStream метода или GetResponse . Timeout Изменение свойства после вызова метода или GetResponse не оказывает никакого GetRequestStream влияния

Свойство Timeout не влияет на асинхронные запросы, выполняемые BeginGetResponse с помощью метода или BeginGetRequestStream .

Внимание!

В случае асинхронных запросов клиентское приложение реализует собственный механизм времени ожидания. См. пример в методе BeginGetResponse .

Чтобы указать время ожидания до истечения времени ожидания операции чтения или записи, используйте ReadWriteTimeout свойство .

Для возврата запроса системы доменных имен (DNS) может потребоваться до 15 секунд. Если запрос содержит имя узла, требующее разрешения, и для него задано Timeout значение менее 15 секунд, может потребоваться 15 секунд или более, чтобы WebException указать время ожидания запроса.

Применяется к

См. также раздел