I\'m building an application to retrieve an image from internet. Even though it works fine, it is slow (on wrong given URL) when using try-catch statements in the applicatio
you can use the function Uri.TryCreate As Panagiotis Kanavos suggested if you like to test and create a url or you can use Uri.IsWellFormedUriString function as suggested by Todd Menier if you just wanted to test the validity of Url. this can by handy if you are just validating user input for now and need to create url some time later in life time of your application.
**But my post is for the People, like myself :( , still hitting their heads against .net 1.1 **
both above methods were introduced in .net 2.0 so you guys still have to use try catch method, which, in my opinion, is still far better than using regular expression.
private bool IsValidHTTPURL(string url)
{
bool result = false;
try
{
Uri uri = new Uri(url);
result = (uri.Scheme == "http" || uri.Scheme == "https");
}
catch (Exception ex)
{
log.Error("Exception while validating url", ex);
}
return result;
}