c# HttpClient timeout code example

Example 1: set request timeout c#

<system.web>
     <httpRuntime executionTimeout="600"/>
 </system.web>

Example 2: WebClient timeout

private class WebClient : System.Net.WebClient
    {
        public int Timeout { get; set; }

        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest lWebRequest = base.GetWebRequest(uri);
            lWebRequest.Timeout = Timeout;
            ((HttpWebRequest)lWebRequest).ReadWriteTimeout = Timeout;
            return lWebRequest;
        }
    }

    private string GetRequest(string aURL)
    {
        using (var lWebClient = new WebClient())
        {
            lWebClient.Timeout = 600 * 60 * 1000;
            return lWebClient.DownloadString(aURL);
        }
    }

Example 3: c# restclient timeout

var client = new RestClient(BaseUrl);
client.Timeout = 5000; // 5000 milliseconds == 5 seconds