What Was I Thinking?

Follies & Foils of .NET Development
posts - 95 , comments - 352 , trackbacks - 0

Supporting Request Timeout with a WebClient

I recently needed to adjust the timeout value of the HTTP Request being generated by WebClient.  My attempt to perform a POST of a large file over HTTP was causing the WebClient to timeout.  I thought adjusting the timeout value for the web client would fix this problem.  One problem, the WebClient doesn’t support a Timeout value.

 

I did some snooping via Reflector and discovered 2 important facts about the WebClient.  Fact #1, The WebClient isn’t sealed.  This means we can inherit from it.  Fact #2: The WebClient factories the HTTP Request from a protected method called GetWebRequest.  This means I’d only need to override the GetWebRequest method to affect the HTTP Request used by the web client.

 

so..

I created the following class:

Code Snippet
  1. public class ExtendedWebClient : WebClient
  2.   {
  3.       public int Timeout { get; set; }
  4.  
  5.       protected override WebRequest GetWebRequest(Uri address)
  6.       {
  7.           WebRequest request = base.GetWebRequest(address);
  8.           if (request != null)
  9.               request.Timeout = Timeout;
  10.           return request;
  11.       }
  12.  
  13.       public ExtendedWebClient()
  14.       {
  15.           Timeout = 100000; // the standard HTTP Request Timeout default
  16.       }
  17.   }

 

When I need to control the timeout of a request, I create an instance of my ExtendedWebClient and set the timeout property:

Code Snippet
  1.  
  2. using (var client = new ExtendedWebClient())
  3. {
  4.     client.Timeout = -1;

 

Let me know if you find this as useful as I did.

Print | posted on Tuesday, September 14, 2010 12:52 PM | Filed Under [ Visual Studio WCF ]

Feedback

Gravatar

# re: Supporting Request Timeout with a WebClient

This code seems to work pretty well. I am very sure that I will implement this in my cazare sinaia website.
10/18/2011 6:35 AM | Eric
Gravatar

# re: Supporting Request Timeout with a WebClient

Man, you saved my life. I was blocked because of webclient lack of time-out setting and didn't want to use a complex code. thank you it's perfect !!
11/15/2011 4:44 AM | mrfaf
Gravatar

# re: Supporting Request Timeout with a WebClient

it seems that you are the only one who has figured out a clean way of solving the timeout problem with the WebClient class. Congratulations and thanks alot, it helped out project to move further :)
7/10/2012 10:43 AM | Traian
Gravatar

# re: Supporting Request Timeout with a WebClient

I've had a symilar problem when trying to upload the online store software to my server. Because it was a large file, the client timed out. With some minor changes in the settings I was able to increase the response time and allow for larger files to be uploaded.
9/19/2012 6:38 AM | DavieAlvin
Gravatar

# re: Supporting Request Timeout with a WebClient

This is great, thank you!
1/14/2013 1:11 PM | Roy Jackson
Gravatar

# re: Supporting Request Timeout with a WebClient

Everybody is so happy!
Where am I going wrong then?
I put the code on my form...
6/6/2013 10:33 AM | Cristian
Gravatar

# re: Supporting Request Timeout with a WebClient

Thnks a lot!

I have used your code to run one php script from task scheduler on windows, without timeout.

I have put here the code if can help any: http://snipplr.com/view/91280/setup-one-timeout-value-for-downloadstring-method-of-webclient-class--in-powershell/
2/22/2015 12:46 PM | Juan Antonio
Gravatar

# re: Supporting Request Timeout with a WebClient

done a gr8 job thanks alot
8/12/2015 7:36 AM | M.fasi
Post A Comment
Title:
Name:
Email:
Comment:
Verification:
 

Powered by: