Skip to content

Instantly share code, notes, and snippets.

@paigecook
Created January 31, 2011 15:09
Show Gist options
  • Save paigecook/804152 to your computer and use it in GitHub Desktop.
Save paigecook/804152 to your computer and use it in GitHub Desktop.
Check if a Url exists
public static bool UrlExists(string url)
{
try
{
var request = WebRequest.Create(url) as HttpWebRequest;
if (request == null) return false;
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
return response.StatusCode == HttpStatusCode.OK;
}
}
catch (UriFormatException)
{
//Invalid Url
return false;
}
catch (WebException)
{
//Unable to access url
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment