HttpWebResponse returns 404 error

Skuta picture Skuta · Dec 7, 2009 · Viewed 21k times · Source

How to let Httpwebresponse ignore the 404 error and continue with it? It's easier than looking for exceptions in input as it is very rare when this happens.

Answer

Adam Maras picture Adam Maras · Dec 7, 2009

I'm assuming you have a line somewhere in your code like:

HttpWebResponse response = request.GetResponse() as HttpWebResponse;

Simply replace it with this:

HttpWebResponse response;

try
{
    response = request.GetResponse() as HttpWebResponse;
}
catch (WebException ex)
{
    response = ex.Response as HttpWebResponse;
}