Featured Article

Master .net Framework HttpClient Timeout Settings Optimize Performance and Avoid Hanging Requests

Kenneth Jul 13, 2026

The .NET Framework's HttpClient is a widely-used class for making HTTP requests in Windows Communication Foundation (WCF) and ASP.NET applications. However, when using HttpClient, you might encounter instances where you want to set a timeout for requests to prevent them from hanging indefinitely. In such cases, understanding how to configure timeouts in HttpClient can be invaluable.

Fix: Windows service hosting WCF service is not starting
Fix: Windows service hosting WCF service is not starting

Before we delve into the details, let's discuss why timeouts are essential. Network instability, server unavailability, or overly complex requests can lead to requests taking longer than expected to complete. If not handled properly, these scenarios can cause your application to become unresponsive or consume excessive resources, impacting your users' experience and your application's performance.

an image of a computer screen with the text loading please wait on it, and there is
an image of a computer screen with the text loading please wait on it, and there is

Understanding HttpClient Timeouts

HttpClient itself does not enforce timeouts. Instead, it relies on the underlying system-wide settings. This means that the timeout duration is subject to the operating system and the network stack it's running on. However, you can override these defaults to suit your application's needs.

Client Challenge
Client Challenge

There are two types of timeouts you can set: a request timeout and a connection timeout. The request timeout determines how long to wait for a response from the server, while the connection timeout sets the duration for establishing a network connection.

Setting Request Timeouts

Metro Fundz
Metro Fundz

To set a request timeout, you can use the ServicePointManager class along with its ServerCertificateValidationCallback and ServiceCertificateValidationCallback properties. Here's an example:

    ServicePointManager.ServerCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
    ServicePointManager.ServiceCertificateValidationCallback = (sender, cert, chain, sslPolicyErrors) => true;
    ServicePointManager.DefaultConnectionLimit = 120;
    var timeout = 10000; // 10 seconds
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls11;
    ServicePointManager.Expect100Continue = false;
    ServicePointManager.MaxServicePointIdleTime = timeout;

In the above code, the DefaultConnectionLimit property sets the maximum number of concurrent connections that can be established with a single server. The MaxServicePointIdleTime property sets the number of milliseconds a service point should remain open even if not in use.

two screens showing text messages from the same person and what they are saying on them
two screens showing text messages from the same person and what they are saying on them

Setting Connection Timeouts

For connection timeouts, you can use the WebRequest class. Here's how you can do it:

    var httpRequest = (HttpWebRequest)WebRequest.Create("http://www.example.com/");
    httpRequest.Timeout = 10000; // 10 seconds
    httpRequest.ReadTimeout = 10000; // 10 seconds

an instruction manual for processing data
an instruction manual for processing data

In this example, the Timeout property sets the total time out for an operation. The ReadTimeout property sets the time to wait for the server to send data.

Handling Timeouts in HttpClient

Web3 Sports Experiences, NFTs, Ticketing & Activations | Be. by Animoca Brands
Web3 Sports Experiences, NFTs, Ticketing & Activations | Be. by Animoca Brands
an image of the average time spent on google's search results for different businesses
an image of the average time spent on google's search results for different businesses
the sign up screen for transferer has started, and it's now available
the sign up screen for transferer has started, and it's now available
MTE Explains: The Difference Between HTTP and HTTPS
MTE Explains: The Difference Between HTTP and HTTPS
Client Challenge
Client Challenge
how tracert / traceroute works diagram
how tracert / traceroute works diagram
Why You Should Not Use Telnet for Remote Connections
Why You Should Not Use Telnet for Remote Connections
an email screen with the text, 9 % update spending need more times card to complete transaction
an email screen with the text, 9 % update spending need more times card to complete transaction
Fix: "Sorry, you've used all your temporary codes" on Netflix | TechLatest
Fix: "Sorry, you've used all your temporary codes" on Netflix | TechLatest

When a request times out, HttpClient throws a WebException with a WebExceptionStatus of Timeout. You can catch this exception and handle it accordingly. Here's how you can do it:

    var client = new HttpClient();
    try
    {
        var response = client.GetAsync("http://www.example.com/").Result;
    }
    catch (WebException ex)
    {
        if (ex.Status == WebExceptionStatus.Timeout)
            // Handle timeout
    }
    catch (Exception ex)
    {
        // Handle other exceptions
    }

In this example, the code attempts to make a GET request to an URL. If the request times out, the WebException with a WebExceptionStatus of Timeout is caught and handled.

In conclusion, mastering HttpClient timeouts allows for more robust and reliable applications. By setting appropriate timeouts, you can prevent your application from hanging indefinitely, improve response times, and enhance the overall user experience. Regularly reviewing and fine-tuning your timeouts can help ensure that your application remains agile and resilient in the face of network fluctuations and server instability.