.NET
WebClient
Timeout
Programming
C#

How to change the timeout on a .NET WebClient object

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

WebClient does not expose a public Timeout property, so changing the timeout requires either subclassing it or choosing a newer HTTP API. The most common workaround is to override GetWebRequest and set the underlying request timeout there, although in modern .NET code HttpClient is usually the better long-term choice.

Subclass WebClient

If you must keep using WebClient, create a derived class and set timeout values on the generated WebRequest.

csharp
1using System;
2using System.Net;
3
4public class TimeoutWebClient : WebClient
5{
6    public int Timeout { get; set; } = 10000;
7
8    protected override WebRequest GetWebRequest(Uri address)
9    {
10        var request = base.GetWebRequest(address);
11
12        if (request != null)
13        {
14            request.Timeout = Timeout;
15
16            if (request is HttpWebRequest httpRequest)
17            {
18                httpRequest.ReadWriteTimeout = Timeout;
19            }
20        }
21
22        return request;
23    }
24}

You can then use it like this:

csharp
using var client = new TimeoutWebClient { Timeout = 5000 };
string body = client.DownloadString("https://example.com");
Console.WriteLine(body.Length);

This is the classic workaround when a project is already committed to WebClient.

Know What Timeout You Are Setting

There are usually two related concerns:

  • connection and response wait time
  • read/write timeout during streaming operations

Setting only request.Timeout may not be enough for all scenarios. That is why the subclass above also sets ReadWriteTimeout when the underlying request is an HttpWebRequest.

Be Careful With Blocking Calls

WebClient is an older API and encourages synchronous code such as DownloadString. That is often enough for quick scripts, but it is less ideal for scalable or UI-sensitive applications.

If the request is slow and the timeout is long, a synchronous call can block the current thread for a noticeable time. That is one reason many .NET codebases moved to HttpClient.

Prefer HttpClient in Newer Code

If you are not forced to use WebClient, HttpClient is usually the cleaner option.

csharp
1using System;
2using System.Net.Http;
3using System.Threading.Tasks;
4
5public static async Task Main()
6{
7    using var client = new HttpClient
8    {
9        Timeout = TimeSpan.FromSeconds(5)
10    };
11
12    string body = await client.GetStringAsync("https://example.com");
13    Console.WriteLine(body.Length);
14}

This gives you a first-class timeout property and a more modern async model.

Timeout Versus Cancellation

Sometimes a global timeout is not flexible enough. In those cases, a cancellation token can be a better control point than relying only on Timeout.

That is another reason HttpClient is usually preferable. It fits naturally with cancellation tokens, async workflows, and modern resilience patterns.

Handle Timeout Failures Explicitly

A timeout is not just a configuration value. It is part of the application's error-handling behavior. Whether you use WebClient or HttpClient, decide what the code should do when a request times out:

  • retry
  • surface an error to the user
  • log and continue
  • back off and try later

Without that decision, changing the timeout only moves the failure point.

Common Pitfalls

The biggest mistake is looking for a public WebClient.Timeout property that does not exist. You only get timeout control by subclassing or by switching APIs.

Another common issue is setting only one timeout and forgetting that streaming operations may also need ReadWriteTimeout.

People also keep adding complexity around WebClient when the simpler fix would be to migrate the call to HttpClient.

Finally, do not treat the timeout value as universally correct. A timeout that is safe for one endpoint may be too short or too long for another.

Summary

  • 'WebClient has no built-in public timeout property.'
  • The standard workaround is to subclass it and override GetWebRequest.
  • Set both Timeout and, when relevant, ReadWriteTimeout.
  • In newer .NET code, prefer HttpClient for cleaner timeout and async support.
  • Choose timeout values as part of overall error-handling design, not just as a magic number.

Course illustration
Course illustration

All Rights Reserved.