Click here to Skip to main content
13,191,197 members (81,534 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


2 bookmarked
Posted 30 Mar 2010

Setting timeout property for System.Net.WebClient Class

, 30 Mar 2010
Rate this:
Please Sign up or sign in to vote.

As the title of the article describes this article is going to describe a very simple but very helpful topic. Every now and then we are faced with a situation where we have to provide functionality to download/upload the files.The WebClient class Provides common methods for sending data to and receiving data from a resource identified by a URI, But the problem is when we try to download a bigger file which takes more time to download we get a Timeout Exception
and Web Client class does not provide any property where we can specify/set the timeout value.

Using the code 

You can make use of the WebRequest class to set the timeout property.The code contains a Wrapper class called ExtendedWebClient which is inherited from the WebClient class and a overrided method of type WebRequest which sets the timeout property.The ExtendedWebClient class can be used as WebClient Class but with the timeout property being set.

public class ExtendedWebClient: WebClient
    private int timeout;
    public int Timeout
           return timeout;
           timeout = value;
    public ExtendedWebClient(Uri address)    
         this.timeout = 600000;//In Milli seconds
         var objWebClient = GetWebRequest(address); 
    protected override WebRequest GetWebRequest(Uri address)
         var objWebRequest= base.GetWebRequest(address);
         objWebRequest.Timeout = this.timeout;
         return objWebRequest;

Now you can just Instantiate the ExtendedWebClientclass nad use all the available methods and properties of WebClient class along with the timeout property being set to 600000 ms. 


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gladson WiIlliam
Software Developer (Senior) Symphony
India India
This member doesn't quite have enough reputation to be able to display their biography and homepage.

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171016.2 | Last Updated 30 Mar 2010
Article Copyright 2010 by Gladson WiIlliam
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid