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.
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.
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.