Grzenio Grzenio - 3 months ago 34
C# Question

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

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

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);
Comments