Set timeout for webClient.DownloadFile
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
The WebClient class in .NET does not expose a Timeout property, which makes it difficult to control how long DownloadFile waits before giving up on a slow or unresponsive server. The default timeout is 100 seconds, inherited from the underlying HttpWebRequest. This article shows several approaches to set a custom timeout for file downloads.
The Problem
If the server is slow, this will block for up to 100 seconds before throwing a WebException.
Solution 1: Subclass WebClient (Quick Fix)
Override GetWebRequest to set the timeout on the underlying HttpWebRequest:
This is the simplest approach and works well for most scenarios.
Solution 2: Use HttpClient (Recommended for .NET Core+)
WebClient is deprecated in .NET 6+. Use HttpClient instead, which has built-in timeout support:
With Progress Reporting
Solution 3: Use HttpWebRequest Directly
For full control over timeouts (connection and read separately):
HttpWebRequest provides two timeout properties:
Timeout: Time to wait for the initial connection and response headersReadWriteTimeout: Time to wait for data during the read/write phase
Solution 4: CancellationToken with HttpClient (Async)
For async code, use a CancellationToken with a timeout:
This approach allows you to cancel the download at any point, not just during connection.
Comparison of Approaches
| Approach | .NET Version | Async | Granular Timeout | Deprecated? |
| WebClient subclass | All | No* | No | Yes (.NET 6+) |
| HttpClient | .NET Core+ | Yes | Via CancellationToken | No |
| HttpWebRequest | All | Optional | Yes (connect + read) | Yes (.NET 6+) |
*WebClient has async methods but they are callback-based, not Task-based.
Common Pitfalls
- WebClient is deprecated: In .NET 6+,
WebClientis marked obsolete. Migrate toHttpClientfor new projects. - HttpClient should be reused: Do not create a new
HttpClientper request — this causes socket exhaustion. UseIHttpClientFactoryor a single static instance. - Timeout vs CancellationToken:
HttpClient.Timeoutapplies to the entire request (including reading the body). For large file downloads, set a generous timeout and useCancellationTokenfor user-initiated cancellation. - Large file memory: Using
GetStringAsyncorReadAsByteArrayAsyncloads the entire file into memory. UseResponseHeadersReadand stream to file for large downloads. - Proxy and DNS: Timeout includes DNS resolution and proxy negotiation. On slow networks, the connection phase alone may consume most of the timeout budget.
Summary
- Subclass
WebClientand overrideGetWebRequestto setTimeoutfor a quick fix - Prefer
HttpClientwithTimeoutproperty for new .NET Core+ projects - Use
HttpWebRequestfor separate connection and read/write timeouts - For large downloads, use streaming with
ResponseHeadersReadto avoid loading the entire file in memory WebClientis deprecated in .NET 6+ — migrate toHttpClient
Related reading
- Set Visual Studio Code to be global Git editor on OSX
- Setting an object to null vs Dispose
- Setting Objects to Null/Nothing after use in .NET
- Setting tab order in WPF
- Setting WPF image source in code
- Settings variable values in a Moq Callback call
- Settings.settings vs. app.config in .NET desktop app
- Setup RabbitMQ consumer in ASP.NET Core application

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.