Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpRequestException vs WebException

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

like image 956
user3063281 Avatar asked Mar 13 '14 14:03

user3063281


People also ask

What is a HttpRequestException?

HttpRequestException(String, Exception, Nullable<HttpStatusCode>) Initializes a new instance of the HttpRequestException class with a specific message that describes the current exception, an inner exception, and an HTTP status code.

What is WebException in c#?

The WebException class is thrown by classes descended from WebRequest and WebResponse that implement pluggable protocols for accessing the Internet. When WebException is thrown by a descendant of the WebRequest class, the Response property provides the Internet response to the application.


2 Answers

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.

like image 124
Darrel Miller Avatar answered Sep 18 '22 12:09

Darrel Miller


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.

like image 33
Rick Avatar answered Sep 21 '22 12:09

Rick