[英]Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()?
I've hunted around for some definitive documentation on this but haven't had much luck finding any. 我已经在寻找一些关于此的明确文件,但没有找到任何运气。
For which HTTP Response Status codes will HttpWebRequest.GetResponse()
generate a WebException
after doing something like a POST? 对于哪些HTTP响应状态代码,
HttpWebRequest.GetResponse()
在执行类似POST的WebException
后会生成WebException
?
Specifically, will it generate a WebException
for anything other than status 200 OK
? 具体来说,它
WebException
为状态200 OK
以外的任何其他内容生成WebException
? Or will it only generate a WebException
for say, 400, 404, and 500 (for the sake of argument)? 或者它只会生成一个
WebException
,例如WebException
和500(为了参数)?
I want to know since the server I'm communicating with defines anything other than HTTP 200 OK
coming back as an error condition. 我想知道,因为我正在与之通信的服务器定义除
HTTP 200 OK
之外的任何其他内容作为错误条件返回。 Can I rely on a WebException
being generated for anything other than 200? 我可以依赖为200以外的任何其他东西生成
WebException
吗?
I've currently written my code to check the return status code every time and ensure it's 200 OK
. 我目前已编写代码来检查每次返回状态代码并确保它是
200 OK
。 If it's not, it will take appropriate action—but there's a lot of duplication between that code and the catch block for a WebException
that I'm hoping to clean up. 如果不是,它将采取适当的行动 - 但是那个代码和我希望清理的
WebException
的catch块之间有很多重复。
Any relevant links to documentation would be most appreciated. 任何与文档相关的链接都将非常受欢迎。
Ended up doing an explicit check after the response & catching and checking WebExceptions; 在响应和捕获并检查WebExceptions之后结束执行显式检查; results in some duplicated code but there's no definitive answer on whether a WebException will ALWAYS occur if the status is NOT 200.
导致一些重复的代码,但如果状态为非200,是否总是会发生WebException没有确定的答案。
I think it will, but it sounds like a risky assumption to make. 我认为它会,但这听起来像是一个冒险的假设。 For one thing, the MSDN docs make it clear that
GetResponse
will throw exceptions other than just WebException
. 首先, MSDN文档清楚地表明
GetResponse
将抛出除WebException
之外的异常。 However, I can say for definite from experience that a "304 Not-Modified" response will be thrown as a WebException
. 但是,我可以从经验中明确地说,“304 Not-Modified”响应将作为
WebException
抛出。
All this talk is giving off a whiffy Code Smell; 所有这些谈话都散发出一股狡猾的Code Smell; don't use exceptions to control the flow of execution.
不要使用异常来控制执行流程。 You would be better off handling exceptions appropriately, then explicitly checking the
StatusCode
property for your allowable values. 您最好适当地处理异常,然后显式检查
StatusCode
属性的允许值。
The WebException system is seperate system from the HTTP error system . WebException系统是来自HTTP错误系统的 独立 系统 。 This is mainly because the HTTP errors are returned by the browser or client and the WebException is thrown by the server while building your page.
这主要是因为浏览器或客户端返回HTTP错误,并且在构建页面时服务器会抛出WebException。 By the time an HTTP error is found the page is sent to the client and you won't know about it.
当发现HTTP错误时,页面将被发送到客户端,您将不会知道它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.