13

Surely, surely, surely there is a way to configure the .Net HttpWebRequest object so that it does not raise an exception when HttpWebRequest.GetResponse() is called and any 300 or 400 status codes are returned?

Jon Skeet does not think so, so I almost dare not even ask, but I find it hard to believe there is no way around this. 300 and 400 response codes are valid responses in certain circumstances. Why would we be always forced to incur the overhead of an exception?

Perhaps there is some obscure configuration setting that evaded Jon Skeet? Perhaps there is a completely different type of request object that can be used that does not have this behavior?

(and yes, I know you can just catch the exception and get the response from that, but I would like to find a way not to have to).

Thanks for any help

Community
  • 1
  • 1
James
  • 7,877
  • 7
  • 42
  • 57

4 Answers4

13

If you want to retrieve the error response from 4xx errors you can do it like this:

HttpWebResponse res = null;
string response = string.Empty;
StreamReader sr = null;
Stream resst = null;
try
{
    res = (HttpWebResponse)req.GetResponse();
    resst = res.GetResponseStream();
    sr = new StreamReader(resst);
    response = sr.ReadToEnd();
}
catch (WebException exception)
{
    HttpWebResponse errorResponse = (HttpWebResponse)exception.Response;
    resst = errorResponse.GetResponseStream();
    sr = new StreamReader(resst);
    response = sr.ReadToEnd();
}
this.Response.Write(response);

Hope this helps...

dgivoni
  • 535
  • 2
  • 7
  • 17
  • this answer http://stackoverflow.com/a/2676411/671619 says the opposite. who is right? – Firo Mar 02 '12 at 14:44
  • 1
    I just ran that code after getting a 400 and it DOES return the expected error details in the response. – rob Mar 08 '13 at 15:38
  • 1
    Beware, [WebException.Response may be null](https://msdn.microsoft.com/query/dev11.query?appId=Dev11IDEF1&l=EN-US&k=k%28System.Net.WebException.Response%29;k%28TargetFrameworkMoniker-.NETFramework). – Oliver Bock Feb 04 '15 at 03:19
6

According to the specification when a server sends a 400 status code it means that:

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

So it looks pretty natural to have an exception in this case. As far as 300 is concerned it is more debatable.

Anyway, if you want some non-standard behavior you can always resort to a TcpClient but that really seems like an extreme and desperate measure.

Did you perform performance tests? Have you confirmed that throwing an exception in this exceptional case is a bottleneck to your application? It seems like a micro optimization in this case. Can't you make this web server happy by forging a valid request and getting 200 at the end? If not can't you switch to a more standard web server?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Hi Darin Thanks for your response. You make good points. I feel that exceptions (due to their expense) should be optional, and am surprised there is not way to circumvent this. Your work-arounds are good ideas. I am going to investigate further... – James Apr 26 '10 at 14:00
  • I agree and importantly web services commonly return 400 and 500 status codes to report invalid inputs to service calls. In many cases these are not errors. For example we get 400 status codes returned from a certain web service just to indicate that there is no record available - it's not an error though. The body of the response is still json with a message for us. So I agree it would be ideal to turn off exceptions which is why I and many others are finding this post. – Action Dan Apr 21 '16 at 00:29
  • The problem is that sometimes the response's content contains critical debugging information about the error (like WHY you got 400), and you don't get that in the exception. – Ohad Schneider Mar 24 '19 at 13:29
5

The best answer:

Fixing WebRequest’s Desire To Throw Exceptions Instead Of Returning Status

WayBack Machine archive of: http://fearthecowboy.com/2011/09/02/fixing-webrequests-desire-to-throw-exceptions-instead-of-returning-status/

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
user1766151
  • 86
  • 1
  • 4
1

Setting the AllowAutoRedirect property on HttpWebRequest to false will stop an exception from being thrown when a server sends a 300 status code.

It doesn't prevent 404 status codes from throwing an exception, though.

Jahan Afshari
  • 111
  • 1
  • 6