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?
相关问题
- Angular RxJS mergeMap types
- Generic Generics in Managed C++
- How to Debug/Register a Permanent WMI Event Which
- 'System.Threading.ThreadAbortException' in
- Bulk update SQL Server C#
相关文章
- C#使用http访问网络,有办法用指定网卡访问网络嘛?
- .net中MessageBox.Show使用问题
- IdentityServer 报错:"idp claim is missing"
- 在 IdentityServer 中如何给 id token 添加更多信息
- IdentityServer 的 Selector 在哪个 nuget 包
- 使用 IdentityServer 的项目遭遇错误:"IDX20803: Unable to obt
- ASP.NET Core ConfigureServices 中从 appsettings.json
- .netCore 控制台程序输出配置问题
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/
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:
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:Ok, this seems to be a by-design behavior and a perfect example of a vexing exception. This can be solved with this:
The way to avoid this
System.WebException
is to set AllowAutoRedirect property tofalse
. This disables the automatic redirection logic of theWebRequest
. 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 requests3xx
have to be handled manually.