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.