1

The Microsoft System.Net.Browser.ClientHttpWebRequest appears to be throwing a WebException with a status code of NotFound, which corresponds to a 404 response by the server, when accessing an invalid IP address at which there is no server running. The correct response is not an HTTP status code and definitely not in the 400-range, since the client library never had an opportunity to speak with a valid HTTP server. This is problematic for my code, because 404 has a special meaning when returned by my server endpoint.

Is there a way to determine when a 404 (NotFound) response was actually returned by the server vs. being used incorrectly to describe a general connectivity failure with the System.Net.Browser.ClientHttpWebRequest class?

Note that this behavior occurs for me on WP8.

esilver
  • 27,713
  • 23
  • 122
  • 168
  • I've been able to handle this specific case in one of my app with the `HttpClient`, by checking the reason phrase that comes along with the error (if it's empty, then it's likely to be a connectivity issue). I don't know whether it's possible to do the same thing with the `HttpWebRequest`. – Kevin Gosse Mar 13 '14 at 19:50
  • Your Internet Service Provider does that. Its DNS server redirects you to a junk page they own. Try `http://1.1.1.1` in your web browser to see the same page. Find a better ISP or change the DNS address to, say, 8.8.8.8 – Hans Passant Mar 13 '14 at 22:23

1 Answers1

1

I had the same problem and catching the System.Net.WebException and inserting the below lines of code inside the catch statement enabled me to get the actual response returned by the server:

catch(System.Net.WebException we)
{
   using (var streamResponse = we.Response.GetResponseStream())
   {
      using (var streamRead = new StreamReader(streamResponse))
      {
         string responseString = streamRead.ReadToEnd();

         // do something with responseString
      }
   }
}
}
ilke.uygun
  • 129
  • 2
  • 9