title: HttpWebRequest throws "The remote server returned an error: (404) Not Found." for 404
body: <p>I am trying to issue a web-request:</p>
  
  <pre><code>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();
     }
  }
  </code></pre>
  
  <p>The problem is that the line:</p>
  
  <pre><code>request.GetResponse();
  </code></pre>
  
  <p>throws an exception:</p>
  
  <ul>
  <li><strong>System.Net.HttpWebResponse</strong>
  
  <ul>
  <li><em>"The remote server returned an error: (404) Not Found."</em></li>
  </ul></li>
  </ul>
  
  <p>Why is <code>HttpWebRequest</code> throwing an exception; and how do i get it to stop?</p>
  
  <h1>Alternatively</h1>
  
  <p>Can someone suggest a method, or .NET class, that <strong>can</strong> let me talk to a web-server, and retrieve the <strong><em>perfectly valid not exceptional</em></strong> 404.</p>
  
  <p><a href="https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netframework-4.8" rel="nofollow noreferrer">HttpClient</a> has its own <a href="https://stackoverflow.com/questions/10679214/how-do-you-set-the-content-type-header-for-an-httpclient-request">issues</a> that remove it from consideration.</p>