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

发布于 2024-09-03 07:19:40 字数 556 浏览 8 评论 0原文

我一直在寻找一些关于这方面的明确文档,但没有找到任何相关的运气。

对于哪些 HTTP 响应状态代码,在执行 POST 之类的操作后,HttpWebRequest.GetResponse() 会生成 WebException

具体来说,除了状态 200 OK 之外,它是否会生成 WebException ?或者它只会为 400、404 和 500 生成 WebException(为了论证)?

我想知道,因为我正在通信的服务器定义了除 HTTP 200 OK 以外的任何内容作为错误条件返回。我可以依赖为除 200 之外的任何内容生成的 WebException 吗?

我目前已编写代码来每次检查返回状态代码并确保其为 200 OK。如果不是,它将采取适当的操作,但该代码和我希望清理的 WebException 的 catch 块之间存在大量重复。

任何相关的文档链接将不胜感激。

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?

Specifically, will it generate a WebException for anything other than status 200 OK? Or will it only generate a WebException for say, 400, 404, and 500 (for the sake of argument)?

I want to know since the server I'm communicating with defines anything other than HTTP 200 OK coming back as an error condition. Can I rely on a WebException being generated for anything other than 200?

I've currently written my code to check the return status code every time and ensure it's 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.

Any relevant links to documentation would be most appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鹤仙姿 2024-09-10 07:19:40

最终在响应后进行了明确的检查&捕获并检查 WebException;导致一些重复的代码,但对于如果状态不是 200 是否总是会发生 WebException 没有明确的答案。

Ended up doing an explicit check after the response & catching and checking 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.

箜明 2024-09-10 07:19:40

我认为会的,但这听起来像是一个冒险的假设。一方面,MSDN 文档很明显,GetResponse 会抛出 WebException 以外的异常。但是,根据经验,我可以肯定地说“304 Not-Modified”响应将作为 WebException 抛出。

所有这些谈话都散发着一股难闻的代码气味;不要使用异常来控制执行流程。您最好适当地处理异常,然后显式检查 StatusCode 属性以获得允许的值。

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. However, I can say for definite from experience that a "304 Not-Modified" response will be thrown as a WebException.

All this talk is giving off a whiffy 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.

我很OK 2024-09-10 07:19:40

WebException 系统是独立的系统,与HTTP 错误系统
这主要是因为 HTTP 错误是由浏览器或客户端返回的,而 WebException 是在构建页面时由服务器抛出的。当发现 HTTP 错误时,页面将发送到客户端,而您不会知道这一点。

The WebException system is seperate system from the HTTP error system.
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. By the time an HTTP error is found the page is sent to the client and you won't know about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文