How to get status code from webclient?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
In .NET, WebClient is convenient for simple downloads, but it does not expose the HTTP status code directly as a normal property after every request. To get the status code, you usually either catch a WebException for error responses or subclass WebClient so you can inspect the underlying WebResponse.
Why WebClient Hides the Status Code
WebClient is a high-level wrapper around older networking APIs. It is designed for convenience methods such as DownloadString and UploadString, not for detailed HTTP inspection. That is why developers are often surprised to find that there is no built-in StatusCode property on the class itself.
This simple example downloads content successfully, but gives you no direct status-code access:
If the request succeeds, you get the body. If it fails with an HTTP error, you get an exception.
Read the Status Code from WebException
For non-success responses such as 404 or 500, WebClient throws a WebException. The response is usually still available, and if it is HTTP you can cast it to HttpWebResponse.
This is the simplest way to inspect status codes when you only care about error cases.
Capture Successful Status Codes by Subclassing WebClient
If you want access to the status code even when the request succeeds, subclass WebClient and override GetWebResponse.
This pattern is useful when your code needs to record or branch on 200, 204, or 304 responses without rewriting the call site completely.
Prefer HttpClient in New Code
If you are writing new .NET code, HttpClient is usually the better API because it returns an HttpResponseMessage with the status code already exposed.
That does not change how WebClient works, but it is the cleaner option for new applications.
Handle Redirects and Non-HTTP Responses Carefully
WebClient can also deal with FTP and file URLs, not just HTTP. In those cases, an HttpWebResponse cast is not appropriate. Even within HTTP, automatic redirects can mean the final visible status differs from the first server response unless you disable redirect handling lower in the stack.
That is another reason WebClient is fine for simple tasks, but less ideal for advanced HTTP behavior.
Common Pitfalls
The most common mistake is expecting WebClient to expose a status-code property directly after DownloadString. Another frequent issue is only checking exceptions and then forgetting that successful requests also have meaningful status codes. Developers also sometimes cast ex.Response to HttpWebResponse without checking the actual protocol. Finally, many projects keep extending WebClient for modern HTTP use cases even though HttpClient is better suited to that job.
Summary
- '
WebClientdoes not directly expose HTTP status codes as a built-in convenience property.' - For error responses, catch
WebExceptionand inspect theHttpWebResponse. - For success responses, subclass
WebClientand overrideGetWebResponse. - Use
HttpClientinstead for new code when you need clear access to status and headers. - Cast to
HttpWebResponseonly when the response is actually HTTP.
Related reading
- How to get the cpu usage per thread on windows win32
- How to get the IP address of the docker host from inside a docker container
- How to get token from service account?
- How to handle HTTP OPTIONS requests in Spring Boot?
- How to get the concrete class name as a string?
- How to get the current date/time in Java
- How to get the name of an exception that was caught in Python?
- How to get the RequestBody in an ExceptionHandler Spring REST

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.