HttpRequestException vs WebException

12,222

Solution 1

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.

Solution 2

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.

Share:
12,222

Related videos on Youtube

user3063281
Author by

user3063281

Updated on June 04, 2022

Comments

  • user3063281
    user3063281 almost 2 years

    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().

  • Peter Ritchie
    Peter Ritchie about 10 years
    yes, the HttpRequestException is sometimes a WebException--which should give you access to the status.
  • user3063281
    user3063281 about 10 years
    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.
  • Darrel Miller
    Darrel Miller about 10 years
    @PeterRitchie I believe WebException should never make it to the surface in HttpClient except as an inner exception of either HttpRequestException or IOException.
  • Peter Ritchie
    Peter Ritchie about 10 years
    @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.
  • Darrel Miller
    Darrel Miller about 10 years
    @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.
  • Peter Ritchie
    Peter Ritchie about 10 years
    Yes, the status code comes from the response--if you can't connect to the server, you can't get a status code :)
  • Ohad Schneider
    Ohad Schneider over 9 years
    +1 One thing you forgot though are timeout exceptions (which would confusingly throw a TaskCanceledException: social.msdn.microsoft.com/Forums/en-US/…)
  • Darrel Miller
    Darrel Miller over 9 years
    @OhadSchneider Yes I did. Thanks for bringing it up.
  • user1496062
    user1496062 about 7 years
    Biggest pain is a library that throws a HttpRequestException and the caller needs to know the error code for logging or retry purposes.