How to get StatusCode from HttpRequestException?

I probably miss something obvious.

I am using an HttpClient that throws an HttpRequestException that contains the StatusCode in the message line.

How can I access this StatusCode ?




Edit : More information, I wrote this question in a hurry.

I am using HttpClient to access another API in 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 convert the HttpRequestException into an HttpResponseException sequentially using a custom ExceptionFilterAttribute .

Unfortunately, the HttpRequestException does not carry any additional information that I could use other than the message. I was hoping to expand the StatusCode in raw (int or enum) form.

Looks like I can:

  • Use message to toggle status code (bleh)
  • Or create my version of EncureSuccessStatusCode and throw an exception that is really useful.
+60
Mar 06 '14 at 7:13
source share
4 answers

The status code was passed as part of the string to an HttpRequestException so you cannot recover it from just such exceptions.

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

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

If you are following Microsoft's guidance now, make sure you clearly understand why it is requesting an HttpResponseMessage.EnsureSucessStatusCode call. If you do not call this function, there should be no exceptions.

+28
Mar 06 '14 at 7:42
source

For what it's 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 - remote name substitute -webexception? forum = csharpgeneral

In the case where I need 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; } 
+18
Aug 10 '16 at 20:24
source

As mentioned by others, it is not a good practice to get a StatusCode from an HttpRequestException, the same thing can be done in advance with an HttpResponseMessage.StatusCode after checking the HttpResponseMessage.IsSuccessStatusCode

Anyway, if due to some restriction / requirement you need to read the StatusCode, there may be two solutions

  • Advanced HttpResponseMessage with an explanation of your custom exception here
  • Hack an HttpRequestException.ToString to get a StatusCode. Because the message is a persistent message, fixed by StatusCode and Repharse.

Below is the code in System.Net.Http.HttpResponseMessage Where SR.net_http_message_not_success_statuscode = "The response status code does not indicate success: {0} ({1})."

 public HttpResponseMessage EnsureSuccessStatusCode() { if (!this.IsSuccessStatusCode) { if (this.content != null) { this.content.Dispose(); } throw new HttpRequestException(string.Format(CultureInfo.InvariantCulture, SR.net_http_message_not_success_statuscode, new object[] { (int)this.statusCode, this.ReasonPhrase })); } return this; } 
+3
Nov 17 '15 at 17:13
source

It worked for me

 var response = ex.Response; var property = response.GetType().GetProperty("StatusCode"); if ( property != null && (HttpStatusCode)property.GetValue(response) == HttpStatusCode.InternalServerError) 
+2
Nov 14 '16 at 11:26
source



All Articles