HttpWebRequest.GetResponse throws error on "Not Found"

  • Thread starter Thread starter Dan
  • Start date Start date
D

Dan

When I execute the following with a purposely bogus URL, GetResponse throws
an error before I can retrieve the HttpStatusCode. Why is that?

try

{

HttpWebResponse response = (HttpWebResponse) m_request.GetResponse();

Console.WriteLine("Status Code = " + response.StatusCode);

}

catch(System.Net.WebException ex)

{

Console.WriteLine(ex.ToString()); // Returns "The remote server returned an
error: (404) Not Found"

}
 
Dan said:
When I execute the following with a purposely bogus URL, GetResponse throws
an error before I can retrieve the HttpStatusCode. Why is that?

try

{

HttpWebResponse response = (HttpWebResponse) m_request.GetResponse();

Console.WriteLine("Status Code = " + response.StatusCode);

}

catch(System.Net.WebException ex)

{

Console.WriteLine(ex.ToString()); // Returns "The remote server returned an
error: (404) Not Found"

}


This is what it's supposed to do. You can get the details of the exception
from ex.Status, and sometimes from ex.Response.
 
Back
Top