简体   繁体   English

哪些特定的状态代码会导致HttpWebRequest.GetResponse()抛出WebException?

[英]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.

相关问题 HttpWebRequest.GetResponse方法卡住了特定的网址 - HttpWebRequest.GetResponse method stucks for specific urls C#HttpWebRequest.GetResponse - 如何处理非异常与webexception响应的StatusCode用法? - C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response? 我正在获取WebException:“操作已超时”立即在HttpWebRequest.GetResponse()上 - I am getting WebException: “The operation has timed out” immediately on HttpWebRequest.GetResponse() HttpWebRequest - GetResponse() - WebException ReceiveFailure - HttpWebRequest - GetResponse() - WebException ReceiveFailure 如果服务器返回200以外的其他值,HttpWebRequest.GetResponse()是否总是抛出WebException吗? - Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server? 当HttpWebRequest.GetResponse抛出WebException时,如何读取返回的自定义错误消息? - How do I read a custom error message returned when HttpWebRequest.GetResponse throws a WebException? HttpWebRequest.getResponse() 返回 NULL - HttpWebRequest.getResponse() returning NULL Web服务中的httpwebrequest.getResponse超时 - httpwebrequest.getResponse timesout in webservice C#HttpWebRequest.GetResponse编码URL - C# HttpWebRequest.GetResponse encoding URL 使用HttpWebRequest.GetResponse时出现KeepAliveException - KeepAliveException when using HttpWebRequest.GetResponse
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM