views:

1008

answers:

2

Hi,

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  A: 

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.

barc0de
+2  A: 

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'

cornerback84
But how many possible return codes could there be for the non-exception case when u do get a response back directly? There are are number of different response codes in the 200 range for example. Or is the first response correct here in that only 200 or 0 are possible for the non exception case?
Greg
Answer updated.
cornerback84
Thanks - so this would mean I assume you may het for example: successful response (ie no exception) but a Status Code of 300 where overall then it's still an issue for the user? That is then to check for true success you need to check for WebException + check for status code in 200 range then?
Greg
Yes you might have to check the exceptions and status code to see if you can rectify the problem (for re-direction)
cornerback84