C# HttpWebRequest.GetResponse - how is StatusCode usage handled for a non-exception vs webexception response?

最后都变了- 提交于 2019-12-12 10:51:05

问题


Can someone help clear up the usage of the "StatusCode" property in HttpWebResponse and WebException?

For example it seems that if:

a) there is no exception, then the HttpWebResponse will have a StatusCode that could have some values that indicate both: - success (e.g. OK, Accepted etc) - failure (e.g. UseProxy, RequestTimeout etc)

b) there is a WebExeption throw, which itself has a response object that again has a StatusCode (which I assume is based on the same HttpStatusCode Enumeration.

Question 1 - Is there any consistency in terms of what StatusCode's will trigger a WebException (and you'd pick up the detail within the exception), versus which would come back without an exception but you'd find out the result in the StatusCode of the response object?

Question 2 - Or more specifically what is the pseduo code (or C# code itself) for trying to handle a httpWebRequest.GetResponse call such that you want to differentiate between the categories of responses for the user:

  • proxy settings / proxy issue => so can tell user to fix proxy settings

  • connectivity issue / web-server down => so user is aware of this

  • server side error (e.g. server is there but there is an issue handling the request - e.g content not there) => so user can raise with website manager

  • success case (and I assume this would be more than just the OK) => na (success case)

thanks


回答1:


In my experience the response status code only returns 200 or 0. Anything else comes through the WebException, including proxy errors like 407 or 417.




回答2:


The WebException is thrown whenever the web request cannot be executed successfully. For e.g 400 and 500 series of responses.

WebExcpetion has a property named Status which will return the actual status of the response i.e 500 (Internal Server Error).

Here is the list of all response codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

===============================================================================

In general:

1xx series of code = provisional response. These are not error codes. For e.g the 100 Continue response which tells that client should continue with its request. Usually WebRequest will not return such response, and handle it itself by sending the rest of request.

2xx series of code = Request was successful received, understood and accepted. These are not error codes. For e.g 200 OK

3xx series of code = Further action needs to be taken. Generally this is not error code (usually its for re-direction) for e.g '301 Moved Permanently', which means that the resource being request is moved to a new location, so any further requests by the client should be on the new URL provided in the response.

OR '305 Use Proxy', which according to you results in an Exception.

4xx series of code = Client errors. These can result in exception. for e.g '400 Bad Request' or '401 Unauthorized'

5xx series of code = Server errors. These can result in exception. for e.g '500 Internal Server Error' or '504 Gateway Timeout'



来源:https://stackoverflow.com/questions/2182544/c-sharp-httpwebrequest-getresponse-how-is-statuscode-usage-handled-for-a-non-e

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