HttpWebRequest.Timeout Property
Gets or sets the time-out value for a request.
[Visual Basic] Overrides Public Property Timeout As Integer [C#] public override int Timeout {get; set;} [C++] public: __property int get_Timeout(); public: __property void set_Timeout(int); [JScript] public override function get Timeout() : int; public override function set Timeout(int);
Property Value
The number of milliseconds to wait before the request times out.
Exceptions
| Exception Type | Condition |
|---|---|
| ArgumentOutOfRangeException | The value specified for a set operation was negative and was not Infinite. |
Remarks
Timeout is the number of milliseconds that a synchronous request made with the GetResponse method waits for a response, and the GetRequestStream methods waits for a stream. If a resource does not respond within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus.Timeout.
The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream methods.
CAUTION In the case of asynchronous requests, it is the responsibility of the client application to implement its own timeout mechanism. Refer to the example in the BeginGetResponse method.
Example
[Visual Basic, C#, C++] The following example sets the Timeout property of the HttpWebRequest instance.
[Visual Basic] ' 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 milli seconds. 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) [C#] // 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 milli seconds. 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(); [C++] // Create a new 'HttpWebRequest' Object to the mentioned URL. HttpWebRequest* myHttpWebRequest = dynamic_cast<HttpWebRequest*>(WebRequest::Create(S"http://www.contoso.com")); Console::WriteLine(S"\nThe timeout time of the request before setting the property is {0} milliseconds.", __box(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(S"\nThe timeout time of the request after setting the timeout is {0} milliseconds.", __box(myHttpWebRequest->Timeout)); // A HttpWebResponse object is created and is GetResponse Property of the HttpWebRequest associated with it HttpWebResponse* myHttpWebResponse = dynamic_cast<HttpWebResponse*>(myHttpWebRequest->GetResponse());
[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button
in the upper-left corner of the page.
Requirements
Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard
See Also
HttpWebRequest Class | HttpWebRequest Members | System.Net Namespace