How do I get StatusCode from HttpRequestException?

2019-01-23 10:08发布

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.

4条回答
何必那么认真
2楼-- · 2019-01-23 10:42

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.

查看更多
ら.Afraid
3楼-- · 2019-01-23 10:52

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)requestException.InnerException).Status == WebExceptionStatus.NameResolutionFailure)
    {
        return true;
    }

    return false;
}
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-23 11:01

As mentioned by others as well it's not a good practice to get the StatusCode from HttpRequestException, the same can be done beforehand with HttpResponseMessage.StatusCode after checking HttpResponseMessage.IsSuccessStatusCode

Anyhow if due to some constraint/requirement one has to read StatusCode, There can be two solution

  1. Extended the HttpResponseMessage with your custom exception explained here
  2. Hack on the HttpRequestException.ToString to get the StatusCode, As the message is a constant post fixed by StatusCode and Repharse.

Below is the code in System.Net.Http.HttpResponseMessage Where SR.net_http_message_not_success_statuscode ="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;
    }
查看更多
小情绪 Triste *
5楼-- · 2019-01-23 11:04

This has worked for me

var response = ex.Response;
var property = response.GetType().GetProperty("StatusCode");
if ( property != null && (HttpStatusCode)property.GetValue(response) == HttpStatusCode.InternalServerError)
查看更多
登录 后发表回答