Monday, January 31, 2011

Verify Url Exists

I recently was working on a web site that displayed a lot of images. The image details are stored separately from the images, so it is possible that when searching images to be displayed, that the actual image no longer exists. So I needed some code to validate that the image was actually present. The web site is an ASP.NET application and searched on StackOverflow and found some really good suggestions on how to do this with C#

So I took the best of both of these posts and created the following helper method.


Please feel free to modify this if you find any issues or problems with. 

3 comments:

Anonymous said...

Doesn't work.....

Property or indexer 'System.Net.HttpWebResponse.StatusCode' cannot be assigned to -- it is read only!

Paige Cook said...

Thanks for catching that I was missing an additional equals sign. I am not trying to assign to the HttpWebResponse.StatusCode property, just compare it to HttpStatusCiode.Ok

The code has now been updated.

Anonymous said...

I would rather catching all exceptions and inside the catch, doing a retry using GET instead. Because some servers doesn´t allow HEAD as request method. Try for instance http://www.amazon.com.