HttpWebRequest.GetResponse 在 HTTP 304 上抛出 WebException
当 Web 服务器使用 HTTP 304(未修改)响应 HttpWebRequest.GetResponse()
时,GetResponse()
会抛出 WebException
,如下所示对我来说很奇怪。这是设计使然还是我在这里遗漏了一些明显的东西?
When a web server responds to HttpWebRequest.GetResponse()
with HTTP 304 (Not Modified), GetResponse()
thows a WebException
, which is so very weird to me. Is this by design or am I missing something obvious here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
好吧,这似乎是一种设计行为,也是 令人烦恼的异常。这可以用以下方法解决:
Ok, this seems to be a by-design behavior and a perfect example of a vexing exception. This can be solved with this:
这确实是一个令人沮丧的问题,并且可以通过使用以下扩展方法类并调用 request.BetterGetResponse() 来解决。
您可以在我关于此主题的博客文章中阅读更多相关信息,网址为 http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire -抛出异常而不是返回状态/
This is really a frustrating problem, and can be alternatively worked around by using the following extension method class and calling request.BetterGetResponse()
You read more about it in my blog post on this subject at http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/
避免此
System.WebException
的方法是设置允许自动重定向 属性设置为
false
。这会禁用
WebRequest
的自动重定向逻辑。对于 304 重定向请求来说,它似乎被破坏了,因为它不是最严格意义上的真正的重定向。当然,这意味着其他重定向请求
3xx
必须手动处理。The way to avoid this
System.WebException
is to setAllowAutoRedirect property to
false
.This disables the automatic redirection logic of the
WebRequest
. It seems to be broken for 304 redirection requests, as it is not a real redirection in the strictest sense.Of course that means that the other redirection requests
3xx
have to be handled manually.仅供参考,这是对 Anton Gogolev 的答案 的更新,该答案使用 C#6 (VS2015)
子句。使用调试器时就不那么烦人了,因为它删除了一个捕获点:
Just as an FYI, this is an update to Anton Gogolev's answer that uses the C#6 (VS2015)
when
clause. It's a little less annoying when using a debugger as it removes one catchpoint:我也在代码中遇到了这个问题:
并且看起来,如果远程服务器返回 304 状态,则必须通过抛出此错误或返回自定义 304 将其传递给浏览器,以便浏览器可以返回缓存的响应。否则,您可能会从远程服务器得到空响应。
因此,在我的情况下,对于具有正确缓存处理的正常行为,它应该是这样的:
I also came across to this issue with code:
And it appears that if Remote Server returns 304 status it must be passed to Browser by throwing this error or returning custom 304 so the Browser could return cached response. Otherwise you will probably get empty Response from the Remote Server.
So in my case for normal behaviour with correct Cache handling it should be like: