0

Does HttpWebRequest.GetResponse() always throw a WebException if anything other than 200 is returned by the server?

I saw this question but the answers were not conclusive.

dFlat
  • 819
  • 7
  • 19

1 Answers1

1

No, WebException will only be thrown in certain conditons, mainly because of timeouts and errors while processing the request.

Here is the documentation for HttpWebRequest.GetResponse():

https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx

Note that there are three main reasons why a WebException will be thrown:

Abort was previously called.

-or-

The time-out period for the request expired.

-or-

An error occurred while processing the request.

The bottom two are the most common, and you'll see 400's from these most often. In the documentation, they have a good recommendation in which you can use to try to diagnose the exact cause of the issue:

If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

If you're using an IDE (visual studio) where you can place a breakpoint, examine the response and status. If not, use Console.Writeline() to print out the response or status code and start investigating from there.

as.beaulieu
  • 464
  • 1
  • 10
  • 26
  • thanks. I saw “An error occurred while processing the request” yesterday while researching and wasn’t sure exactly what it implied. Is statusCode != 200 the same as saying “An error occurred while processing the request.”. Or is there a range of http status codes that this method considers errors? – dFlat Jan 24 '18 at 13:50