HttpRequestException vs WebException
Asked Answered
M

3

24

This is a general question that I'm confused about. I thought once a REST request was made, an error would come back via a WebException. In one case I have I'm getting a HttpRequestException, which doesn't allow me to get the HTTP status code.

I'm new to this stuff, but what is the difference between these? Why are there two types? When does one get used as opposed to another?

WebException seems to work well. HttpRequestException seems like a very weak version of it, where it knows the status code (in it's message) but it won't tell me explicitly what it was.

EDIT: I'm using a HttpClient. Specifically calling client.GetStreamAsync().

Morganite answered 13/3, 2014 at 14:50 Comment(2)
You should add the code that throws (so people can know if you're using WebClient, HttpClient, or some other class)Spindry
Sorry, I'm using a HttpClient. Specifically calling client.GetStreamAsync(). But I'm more interested in understanding generally how this works rather than trying to solve a specific code issue, so I don't want to muddy it with much code.Morganite
P
24

There are three distinct failure scenarios:

a) You could not connect to the server or proxy, in which case a HttpRequestException is thrown. Be aware if your server is down and you are running fiddler, you will never see this exception, you will get a 5XX status code.

b) When reading/writing a network stream, there is some kind of interruption you will get an IOException.

c) You will get back a response with a HttpStatusCode with a 4XX/5XX status code. If your client application chooses to call response.EnsureSuccessStatusCode() then a HttpRequestException will be thrown.

If you decide to call EnsureSuccessStatusCode you are making an explicit decision that you don't care about status codes other than the fact that it was success/fail.

If you really need to bubble up an exception and then later handle the status code then I suggest you create your own extension method to replace EnsureSuccessStatusCode and create your own exception that can store the status code. Or preferably, translate the status code into one of a few different exceptions based on the corrective action you wish to take.

Pharaoh answered 24/3, 2014 at 13:56 Comment(2)
+1 One thing you forgot though are timeout exceptions (which would confusingly throw a TaskCanceledException: social.msdn.microsoft.com/Forums/en-US/…)Thigmotropism
@OhadSchneider Yes I did. Thanks for bringing it up.Pharaoh
M
3

WebException Class: The exception that is thrown when an error occurs while accessing the network through a pluggable protocol.

HttpRequestException Class: A base class for exceptions thrown by the HttpClient and HttpMessageHandler classes.

I think the inner exception of a HttpRequestException could be a WebException however I'm not sure it ever is.


Note, a 404, 302 or whatever response other than a 200 (OK) is not an exception. Those responses are perfectly valid HTTP responses.

Mcclanahan answered 13/3, 2014 at 14:57 Comment(7)
yes, the HttpRequestException is sometimes a WebException--which should give you access to the status.Brisket
I saw the MSDN definitions, but that doesn't really explain it, or I'm not savvy enough in this area to understand the nuances of what it's trying to tell me. In my case the HttpRequestException is not a WebException, nor is any InnerException.Morganite
@PeterRitchie I believe WebException should never make it to the surface in HttpClient except as an inner exception of either HttpRequestException or IOException.Pharaoh
@DarrelMiller re-reading my comment, I wasn't clear... that's what I was trying to say; that HttpRequestException sometimes wraps a WebException--which would provide access to a HttpStatusCode.Brisket
@PeterRitchie Except when the HttpRequestException is thrown by the EnsureSuccessStatusCode, in which case there is no WebException or when the WebException is related to a failure to connect, in which case there is no status code either.Pharaoh
Yes, the status code comes from the response--if you can't connect to the server, you can't get a status code :)Brisket
Biggest pain is a library that throws a HttpRequestException and the caller needs to know the error code for logging or retry purposes.Fantasist
R
1

https://github.com/dotnet/runtime/pull/32455 adds StatusCode to HttpRequestException in .NET 5.

Ratiocinate answered 4/12, 2020 at 9:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.