Created
January 31, 2011 15:09
Check if a Url exists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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