How to properly catch a 404 error in .NET
Posted
by Luke101
on Stack Overflow
See other posts from Stack Overflow
or by Luke101
Published on 2010-01-27T18:04:18Z
Indexed on
2010/03/12
0:47 UTC
Read the original article
Hit count: 258
c#
|webrequest
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.
© Stack Overflow or respective owner