Export (0) Print
Expand All

HttpWebRequest.Timeout Property

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

Namespace: System.Net
Assembly: System (in system.dll)

public override int Timeout { get; set; }
/** @property */
public int get_Timeout ()

/** @property */
public void set_Timeout (int value)

public override function get Timeout () : int

public override function set Timeout (value : int)

Property Value

The number of milliseconds to wait before the request times out. The default is 100,000 milliseconds (100 seconds).

Exception typeCondition

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. 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 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.

// 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.", 
    System.Convert.ToString(myHttpWebRequest.get_Timeout()));

// Set the  'Timeout' property of the HttpWebRequest to 10 milli 
// seconds.
myHttpWebRequest.set_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.", 
    System.Convert.ToString(myHttpWebRequest.get_Timeout()));
// A HttpWebResponse object is created and is GetResponse Property 
//of the HttpWebRequest associated with it 
HttpWebResponse myHttpWebResponse = (HttpWebResponse)
    myHttpWebRequest.GetResponse();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft