HttpWebRequest.GetResponse() - what specific status codes cause an exception to be thrown?

Posted by H. Morrow on Stack Overflow See other posts from Stack Overflow or by H. Morrow
Published on 2010-05-31T22:07:49Z Indexed on 2010/05/31 22:13 UTC
Read the original article Hit count: 474

I've hunted around for some definitive documentation on this but haven't had much luck finding any.

Basically - the question is - for which HTTP Status codes coming back from the server will HttpWebRequest.GetResponse() generate a WebException after doing something like say, a POST?

Specifically - will it generate a WebException for anything other than status 200 OK? Or will it only generate a WebException for say, 400, 404, and 500 (for the sake of argument).

I want to know since, the server I'm communicating with defines anything other than HTTP 200 OK coming back as an error condition - and the key is, can I rely on a WebException being generated for anything other than 200? (I've currently written my code so that it'll actually check the return status code everytime to ensure it's 200 OK and if it's not, take appropriate action - but it's alot of duplication between that, and the catch block for a WebException, and I'm hoping to clean it up...)

Any relevant links to documentation would be most appreciated.

Thanks!

© Stack Overflow or respective owner

Related posts about c#

Related posts about exception