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.
Copyright © 2021 Jogjafile Inc.
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():
Note that there are three main reasons why a WebException will be thrown:
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 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.