Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get StatusCode from HttpRequestException?

I'm probably missing something obvious here.

I'm using HttpClient which throws HttpRequestException that contains StatusCode in the Message string.

How can I access that StatusCode?


Edit: More info, I wrote this question in rush.

I'm using HttpClient to access another API within my WebApi project. Yes, I know why I'm calling EnsureSuccessStatusCode(). I want to propagate some errors downstream such as 404 and 403.

All I wanted was to consistently transform HttpRequestException into HttpResponseException using custom ExceptionFilterAttribute.

Unfortunately, HttpRequestException does not carry any extra info I could use besides the message. I was hoping to uncover StatusCode in raw (int or enum) form.

Looks like I can either:

  1. Use the message to switch the status code (bleh)
  2. Or create my version of EnsureSuccessStatusCode and throw exception that's actually usable.
like image 817
Kugel Avatar asked Mar 06 '14 07:03

Kugel


People also ask

What is HttpRequestException in C#?

HttpRequestException(String, Exception) Initializes a new instance of the HttpRequestException class with a specific message that describes the current exception and an inner exception.

What is an HttpRequestException?

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.

What is EnsureSuccessStatusCode?

The idiomatic usage of EnsureSuccessStatusCode is to concisely verify success of a request, when you don't want to handle failure cases in any specific way. This is especially useful when you want to quickly prototype a client.


2 Answers

Status code was passed as part of a string to HttpRequestException so that you cannot recover it from such exceptions alone.

The design of System.Net.Http requires you to access HttpResponseMessage.StatusCode instead of waiting for the exception.

http://msdn.microsoft.com/en-us/library/system.net.http.httpresponsemessage(v=vs.110).aspx

If you are now following the Microsoft guide, make sure you understand clearly why it asks you to call HttpResponseMessage.EnsureSucessStatusCode. If you don't call that function, there should be no exception.

like image 126
Lex Li Avatar answered Sep 21 '22 02:09

Lex Li


For what its worth, this guy did something clever: https://social.msdn.microsoft.com/Forums/vstudio/en-US/dc9bc426-1654-4319-a7fb-383f00b68def/c-httpresponsemessage-throws-exception-httprequestexception-webexception-the-remote-name?forum=csharpgeneral

In the case where I needed an exception status property, I can do this:

catch (HttpRequestException requestException) {     if (requestException.InnerException is WebException webException && webException.Status == WebExceptionStatus.NameResolutionFailure)     {         return true;     }      return false; } 
like image 40
Steve Avatar answered Sep 21 '22 02:09

Steve