public static void Main()
public static bool IsUrlValid(string url)
public static bool UrlChecker1(string url)
bool tryCreateResult = Uri.TryCreate(url, UriKind.Absolute, out uriResult);
if (tryCreateResult==true && uriResult!=null)
public static bool UrlChecker2(string url)
return Uri.IsWellFormedUriString("https://www.google.com", UriKind.Absolute);
public static int VerifyUrlChecker() {
int invalidUrlsMissed = 0;
Console.WriteLine("Verifying Url Checker...");
foreach (string url in validUrls)
if (IsUrlValid(url)==false)
Console.WriteLine(" Error - tested invalid, should be valid:");
Console.WriteLine(" " + url);
foreach (string url in invalidUrls)
if (IsUrlValid(url)==true)
Console.WriteLine(" Error - tested valid, should be invalid:");
Console.WriteLine(" " + url);
var totalErrors = invalidUrlsMissed + validUrlsMissed;
Console.WriteLine("------------------------");
Console.WriteLine("Urls Checked:\t\t" + (validUrls.Length + invalidUrls.Length));
Console.WriteLine("Validation errors:\t" + totalErrors);
static string[] validUrls = new string[] {
"http://foo.com/blah_blah",
"http://foo.com/blah_blah/",
"http://foo.com/blah_blah_(wikipedia)",
"http://foo.com/blah_blah_(wikipedia)_(again)",
"http://www.example.com/wpstyle/?p=364",
"https://www.example.com/foo/?bar=baz&inga=42&quux",
"http://userid:password@example.com:8080",
"http://userid:password@example.com:8080/",
"http://userid@example.com",
"http://userid@example.com/",
"http://userid@example.com:8080",
"http://userid@example.com:8080/",
"http://userid:password@example.com",
"http://userid:password@example.com/",
"http://142.42.1.1:8080/",
"http://foo.com/blah_(wikipedia)#cite-1",
"http://foo.com/blah_(wikipedia)_blah#cite-1",
"http://foo.com/unicode_(✪)_in_parens",
"http://foo.com/(something)?after=parens",
"http://☺.damowmow.com/",
"http://code.google.com/events/#&product=browser",
"http://foo.bar/?q=Test%20URL-encoded%20stuff",
"http://-.~_!$&'()*+,;=:%40:80%2f::::::@example.com",
static string[] invalidUrls = new string[] {
"http://foo.bar?q=Spaces should be encoded",
"http:// shouldfail.com",
"http://foo.bar/foo(bar)baz quux",
"http://-error-.invalid/",