HttpWebRequest.GetResponse throws error on "Not Found"

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"

}
 
J

John Saunders

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top