Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 4
System.Net
 Timeout Property
Collapse All/Expand All Collapse All
.NET Framework Class Library
HttpWebRequest..::.Timeout Property

Gets or sets the time-out value in milliseconds for the GetResponse and GetRequestStream methods.

Namespace:  System.Net
Assembly:  System (in System.dll)
Visual Basic
Public Overrides Property Timeout As Integer
C#
public override int Timeout { get; set; }
Visual C++
public:
virtual property int Timeout {
    int get () override;
    void set (int value) override;
}
F#
abstract Timeout : int with get, set
override Timeout : int with get, set

Property Value

Type: System..::.Int32
The number of milliseconds to wait before the request times out. The default value is 100,000 milliseconds (100 seconds).
ExceptionCondition
ArgumentOutOfRangeException

The value specified is less than zero and is not Infinite.

Timeout is the number of milliseconds that a subsequent synchronous request made with the GetResponse method waits for a response, and the GetRequestStream method waits for a stream. The Timeout applies to the entire request and response, not individually to the GetRequestStream and GetResponse method calls. If the resource is not returned within the time-out period, the request throws a WebException with the Status property set to WebExceptionStatus..::.Timeout.

The Timeout property must be set before the GetRequestStream or GetResponse method is called. Changing the Timeout property after calling the GetRequestStream or GetResponse method has no effect

The Timeout property has no effect on asynchronous requests made with the BeginGetResponse or BeginGetRequestStream method.

Caution noteCaution

In the case of asynchronous requests, the client application implements its own time-out mechanism. Refer to the example in the BeginGetResponse method.

To specify the amount of time to wait before a read or write operation times out, use the ReadWriteTimeout property.

A Domain Name System (DNS) query may take up to 15 seconds to return or time out. If your request contains a host name that requires resolution and you set Timeout to a value less than 15 seconds, it may take 15 seconds or more before a WebException is thrown to indicate a timeout on your request.

The following code example sets the Timeout property of the HttpWebRequest object.

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 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)
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 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();
Visual 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 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() );

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2012 Microsoft. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker