How can I change the time limit for webClient.UploadData()?

Grzenio picture Grzenio · Aug 6, 2009 · Viewed 18.2k times · Source

I am using WebClient.UploadData() to do a post on a Java server. How can I extend the time limit? (It times out every time I am trying to do some debugging)

Answer

AnthonyWJones picture AnthonyWJones · Aug 6, 2009

The WebClient doesn't have a timeout property, however it is possible to inherit from the WebClient to give access to Timeout on the internal WebRequest used:

 public class WebClientEx : WebClient
 {
     public int Timeout {get; set;}

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

Usage:

 var myClient = new WebClientEx();
 myClient.Timeout = 900000 // Daft timeout period
 myClient.UploadData(myUri, myData);