简体   繁体   中英

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

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 (eg OK, Accepted etc) - failure (eg 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 (eg server is there but there is an issue handling the request - eg 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

根据我的经验,响应状态代码仅返回200或0.其他任何内容都来自WebException,包括代理错误,如407或417。

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

WebExcpetion has a property named Status which will return the actual status of the response ie 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 eg 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 eg 200 OK

3xx series of code = Further action needs to be taken. Generally this is not error code (usually its for re-direction) for eg '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 eg '400 Bad Request' or '401 Unauthorized'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM