0

I am trying to issue a web-request:

String url = @"http://stackoverflow.com/aaaaaaaa.html";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

String responseText;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
   if (response.StatusCode == HttpStatusCode.NotFound) //404
      return "";

   using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
   {
      responseText = responseStream.ReadToEnd();
   }
}

The problem is that the line:

request.GetResponse();

throws an exception:

  • System.Net.HttpWebResponse
    • "The remote server returned an error: (404) Not Found."

Why is HttpWebRequest throwing an exception; and how do i get it to stop?

Alternatively

Can someone suggest a method, or .NET class, that can let me talk to a web-server, and retrieve the perfectly valid not exceptional 404.

HttpClient has its own issues that remove it from consideration.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219
  • Is `http://stackoverflow.com/aaaaaaaa.html` what you're pointing to on your end, or is it just a placeholder for your question? – Dortimer Sep 03 '19 at 18:48
  • 1
    It is a known feature that `HttpClient` will throw an exception when it doesn't receive a 2xx status code. I'm looking for a good reference post. – gunr2171 Sep 03 '19 at 18:48
  • @Dortimer It's a url that i know does not exist, so that you can try the code for yourself and confirm that 400 (and possibly even 500) level returned status codes cause an exception to be thrown rather than no exception to be thrown. If you like you can substitute your own page that you know returns `404 Not Found` along with the page content. – Ian Boyd Sep 03 '19 at 18:51
  • 1
    You can just catch the webexception and get the status from it and handle it however you like, see [link](https://stackoverflow.com/a/4700154/11885547). What issues are you referring to with the HttpClient? Based on the linked page it's not clear to me. – vdL Sep 03 '19 at 19:35

1 Answers1

0

Here's some code that I wrote before that you're welcome to use that deals with this scenario. If an exception is thrown, you can get the status code by catching the WebException.

public class WebResult
{
    public string Response { get; set; }
    public bool WasSuccessful { get; set; }
    public HttpStatusCode? StatusCode { get; set; }
}

public WebResult GetUrlContents(string url)
{
    try
    {
        var request = (HttpWebRequest)WebRequest.Create(url);

        using (var response = (HttpWebResponse)request.GetResponse())
        using (var responseStream = new StreamReader(response.GetResponseStream()))
        {
            return new WebResult
            {
                WasSuccessful = true,
                Response = responseStream.ReadToEnd(),
                StatusCode = response.StatusCode
            };
        }
    }
    catch (WebException webException)
    {
        return new WebResult
        {
            Response = null,
            WasSuccessful = false,
            StatusCode = (webException.Response as HttpWebResponse)?.StatusCode
        };
    }
}
Merkle Groot
  • 846
  • 5
  • 9