Which specific status codes cause a WebException to be thrown by HttpWebRequest.GetResponse()?

空扰寡人 提交于 2019-12-04 02:32:27

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.

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!