How to fix a WebClient timeout issue

The issue occurs when the time for generating the whole response of a WebClient request is longer than the default timeout (2 minutes / 60000 miliseconds) . So the solution is to increase this timout value. The WebClient class doesn't have such property or method, so you have 2 possibilities:

1. Use HttpWebRequest instead of WebClient and set its timeout by ReadWriteTimeout property. Of course the usage of this class is more complex than WebClisnt's one but it gives you more flexibility.

2. Make a derived class (wrapper) , which willset the timeout propery of the base class of the WebClient.


public class MyWebClient: WebClient
{
//time in milliseconds
private int timeout;
public int Timeout
{
get {
return timeout;
}
set {
timeout = value;
}
}

public
MyWebClient()
{
this.
timeout = 60000;
}


public MyWebClient(int timeout)
{
this.
timeout = timeout;
}

protected override WebRequest GetWebRequest(Uri address)
{
var result = base.GetWebRequest(address);
result
.Timeout = this.timeout;
return result;
}
}



The usage is pretty much the same as the WebClient's usage but we have to specify the new timeout in the constructor or by setting the Timeout property:

MyWebClient wClient = new MyWebClient(1800000);

UTF8Encoding objUTF8 = new UTF8Encoding();

try
{
byte[] data =
wClient.DownloadData(url);

File.WriteAllBytes(
System.AppDomain.CurrentDomain.BaseDirectory + @"\files\output.xls", data);

wClient = null;
objUTF8 = null;
}
catch (Exception ex)
{
objWebClient = null;
objUTF8 = null;

}

As you see this timeout property solves the problems by client side. The server response should also have longer timeout for generating the response. It is set by machine.config or web.config - httpRuntime section. Note that the timeout in web.config is specified in seconds.

10 comments:

  1. thank you! thank you! thank you!


    In case someone is in need of the VB.NET version, here it goes :


    -----------------------------------

    Imports System.Net

    Public Class MyWebClient
    Inherits WebClient

    Private _timeout As Integer

    Public Property timeout As Integer

    Get
    timeout = _timeout
    End Get

    Set(ByVal value As Integer)
    _timeout = value
    End Set
    End Property

    Public Sub MyWebClient()
    Me.timeout = 60000
    End Sub

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
    Dim result = MyBase.GetWebRequest(address)
    result.Timeout = Me._timeout
    Return result
    End Function

    End Class
    -----------------------------

    In order to set the timeout I used the following:

    Dim objWebClient As MyWebClient = New MyWebClient()
    ... ... ...
    objWebClient.timeout = 3600000
    objWebClient.DownloadFile(ObjUri, TargetFile)

    ReplyDelete
  2. The class MyWebClient doesn't work. I've disconnected my lan cable to see whether or not a timeout occurs and nothing happend.

    ReplyDelete
  3. If timeout beound is reached you should get the respective exception. You if you haven't done any code changes and disconnect from network you should see that exception after two minutes.

    Please check your request timeout setting in web config and make sure it is longer than the webrequest timeout. Otherwise it you will rather see a general request timeout exception.

    system.web/httpRuntime -> executionTimeout="90"
    the setting define the page request timeout in seconds. I think the default value is 90 seconds.

    Hope this helps.

    ReplyDelete
  4. Replies
    1. You will need to also increase the request execution timeout in web.config
      Ex.


      The execution timout must be bigger than the WebClient request timeout.

      Delete
    2. <httpRuntime executionTimeout="600" >

      the tag was not properly shown in my previous commnet

      Delete
  5. Thank you, this was useful.

    p.s. You don't need to define the Timeout property of MyWebClient. You are not using it. The lowercase timeout is enough.

    ReplyDelete


  6. Maxed Out settings, but still not long enough for long processing

    ReplyDelete
    Replies
    1. You will need to also increase the request execution timeout in web.config
      Ex.


      The execution timout must be bigger than the WebClient request timeout.

      Delete