我如何从HttpRequestException得到的StatusCode?

编程入门 行业动态 更新时间:2024-10-25 15:28:38
本文介绍了我如何从HttpRequestException得到的StatusCode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可能失去了一些东西明显在这里。

我使用的HttpClient会抛出HttpRequestException包含的StatusCode在消息字符串。

我如何访问的StatusCode

修改:更多的信息,我在匆忙写了这个问题

我使用的HttpClient我的WebAPI项目内访问另一个API。是的,我知道为什么我打电话 EnsureSuccessStatusCode()。我想传播一些错误的下游,如404 403。

所有我想要的是始终如一地变换 HttpRequestException 到 HttpResponseException 使用自定义的 ExceptionFilterAttribute 。

不幸的是,HttpRequestException不携带任何额外的信息,我可以除了消息中使用。我希望能揭开的StatusCode在原始(int或枚举)的形式

看起来像我可以:

  • 使用信息切换状态代码(的Bleh)
  • 或者创建我的版本EnsureSuccessStatusCode,并抛出异常实际上是可用的。
  • 解决方案

    状态代码作为字符串的一部分传递到 HttpRequestException 这样就可以不要单凭这样的例外恢复它。

    System.Net.Http的设计需要您访问 HttpResponseMessage.StatusCode 而不是等待例外。

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

    如果你现在下面的微软指南,一定要清楚地了解为什么它会要求你打电话 HttpResponseMessage.EnsureSucessStatusCode 。如果你不调用该函数,不应该有例外。

    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 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:

  • Use the message to switch the status code (bleh)
  • Or create my version of EnsureSuccessStatusCode and throw exception that's actually usable.
  • 解决方案

    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.

    msdn.microsoft/en-us/library/system.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.

    更多推荐

    我如何从HttpRequestException得到的StatusCode?

    本文发布于:2023-11-26 15:20:34,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1634252.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:HttpRequestException   StatusCode

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!