问题描述
我可能失去了一些东西明显在这里。
我使用的HttpClient会抛出HttpRequestException包含的StatusCode在消息字符串。
我如何访问的StatusCode
修改:更多的信息,我在匆忙写了这个问题
我使用的HttpClient我的WebAPI项目内访问另一个API。是的,我知道为什么我打电话 EnsureSuccessStatusCode()
。我想传播一些错误的下游,如404 403。
所有我想要的是始终如一地变换 HttpRequestException
到 HttpResponseException
使用自定义的 ExceptionFilterAttribute
。
不幸的是,HttpRequestException不携带任何额外的信息,我可以除了消息中使用。我希望能揭开的StatusCode在原始(int或枚举)的形式
看起来像我可以:
- 使用信息切换状态代码(的Bleh)
- 或者创建我的版本EnsureSuccessStatusCode,并抛出异常实际上是可用的。
状态代码作为字符串的一部分传递到 HttpRequestException
这样就可以不要单凭这样的例外恢复它。
System.Net.Http的设计需要您访问 HttpResponseMessage.StatusCode $ C $的c>而不是等待例外。
的,一定要清楚地了解为什么它会要求你打电话 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.
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.
这篇关于我如何从HttpRequestException得到的StatusCode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!