System.Net.WebException HTTP 状态代码
有没有一种简单的方法可以从 System.Net.WebException 获取 HTTP 状态代码?
Is there an easy way to get the HTTP status code from a System.Net.WebException
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
(我确实意识到这个问题很老了,但它是 Google 上最热门的问题之一。)
您想知道响应代码的常见情况是在异常处理中。从 C#7 开始,您可以使用模式匹配实际上仅在异常与您的谓词匹配时才输入 catch 子句:
这可以轻松扩展到更高的级别,例如在
WebException
的情况下实际上是另一个内部异常(我们只对404
感兴趣):最后:请注意,当它与您的条件不匹配时,无需在 catch 子句中重新抛出异常,因为我们在上述解决方案中首先没有输入该子句。
(I do realise the question is old, but it's among the top hits on Google.)
A common situation where you want to know the response code is in exception handling. As of C# 7, you can use pattern matching to actually only enter the catch clause if the exception matches your predicate:
This can easily be extended to further levels, such as in this case where the
WebException
was actually the inner exception of another (and we're only interested in404
):Finally: note how there's no need to re-throw the exception in the catch clause when it doesn't match your criteria, since we don't enter the clause in the first place with the above solution.
仅当 WebResponse 是 HttpWebResponse 时这才有效。
this works only if WebResponse is a HttpWebResponse.
您可以尝试使用此代码从 WebException 获取 HTTP 状态代码。它也适用于 Silverlight,因为 SL 没有定义 WebExceptionStatus.ProtocolError。
You can try this code to get HTTP status code from WebException. It works in Silverlight too because SL does not have WebExceptionStatus.ProtocolError defined.
我不确定是否有,但如果有这样的财产,它就不会被认为是可靠的。
WebException
可能因 HTTP 错误代码以外的原因而被触发,包括简单的网络错误。那些没有这样匹配的http错误代码。您能否向我们提供更多有关您尝试使用该代码完成的任务的信息。可能有更好的方法来获取您需要的信息。
I'm not sure if there is but if there was such a property it wouldn't be considered reliable. A
WebException
can be fired for reasons other than HTTP error codes including simple networking errors. Those have no such matching http error code.Can you give us a bit more info on what you're trying to accomplish with that code. There may be a better way to get the information you need.
也许是这样的……
Maybe something like this...
通过使用空条件运算符 (
?.)您可以使用一行代码获取 HTTP 状态代码:
变量
status
将包含HttpStatusCode
。当出现更常见的故障(例如网络错误)且未发送任何 HTTP 状态代码时,status
将为 null。在这种情况下,您可以检查ex.Status
以获取WebExceptionStatus
。如果您只想在发生故障时记录描述性字符串,可以使用 null -coalescing 运算符 (
??
) 来获取相关错误:如果由于 404 HTTP 状态代码而引发异常,则字符串将包含“NotFound”。另一方面,如果服务器离线,则字符串将包含“ConnectFailure”等。
By using the null-conditional operator (
?.
) you can get the HTTP status code with a single line of code:The variable
status
will contain theHttpStatusCode
. When the there is a more general failure like a network error where no HTTP status code is ever sent thenstatus
will be null. In that case you can inspectex.Status
to get theWebExceptionStatus
.If you just want a descriptive string to log in case of a failure you can use the null-coalescing operator (
??
) to get the relevant error:If the exception is thrown as a result of a 404 HTTP status code the string will contain "NotFound". On the other hand, if the server is offline the string will contain "ConnectFailure" and so on.