21

Possible Duplicate:
How can I catch a 404?

I would like to know the proper way to catch a 404 error with c# asp.net here is the code I'm using

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(String.Format("http://www.gravatar.com/avatar/{0}?d=404", hashe));

// execute the request
try
{
    //TODO: test for good connectivity first
    //So it will not update the whole database with bad avatars
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    Response.Write("has avatar");
}
catch (Exception ex)
{
    if (ex.ToString().Contains("404"))
    {
         Response.Write("No avatar");
    }
}

This code works but I just would like to know if this is the most efficient.

Community
  • 1
  • 1
Luke101
  • 59,479
  • 80
  • 216
  • 348

0 Answers0