WebClient with Timeout Support

By default the C# WebClient class does not let you modify the default timeout value of 1 minute. This subclass exposes the Timeout property so you can change the default without having to use a HttpWebRequest.

using System;
using System.Net;
 
namespace MyNamespace
{
    public class WebClientEx : WebClient
    {
        public int Timeout { get; set; }
 
        public WebClientEx()
        {
            Timeout = 60000;
        }
 
        public WebClientEx(int timeout)
        {
            Timeout = timeout;
        }
 
        protected override WebRequest GetWebRequest(Uri address)
        {
            WebRequest request = base.GetWebRequest(address);
            request.Timeout = Timeout;
            return request;
        }
    }
}

Leave a Reply

Your email address will not be published.