tags:

views:

1956

answers:

5

In one of the previous posts it was suggested to use System.Uri to check if the URL is valid. How does one do that?

A: 
try
{
    System.Uri test = new System.Uri(stringToTest);
}
catch (FormatException ex)
{
    return false;
}
return true;
John Saunders
really? the only way to check is to see if it throws an exception? i thought throwing exception for purposes of performing an assertion is a bad programming practice.
dev.e.loper
I didn't throw the exception - System.Uri constructor did.
John Saunders
it doesn't matter who threw the exception, the idea is the same. idea of using try/catch block instead of if/else statement. ideally you don't want anything to throw an exception because it is an expensive process. hence there are functions like Url.TryCreate which don't throw exception.
dev.e.loper
exceptions are no that expensive, and if you care a lot about performance read http://blogs.msdn.com/ricom/ , the problem with the Uri class is that only works well with absolute Uris, TryCreate returns true with "/folder/{ht.com.m\\/sx.r:erp://", and also new Uri("/folder/{ht.com.m\\/sx.r:erp://") don't throw any exception
Juan Zamudio
It's not only exception is wrong or right, this is more like abusing exception. There is a lovely method called "TryCreate" just for this.
dr. evil
@All: If I admit that I didn't know about TryCreate, will you all go away and stop discussing this? It's getting tedious.
John Saunders
just because you asked nicely, but its a bit strange that an MVP who has worked with the technology since the early beta versions did not know about TryCreate since is based on the TryParse pattern.
Juan Zamudio
A: 

If you are checking to see if the structure of the URL is valid, then the previous answer is just fine.

However, if you want to check that the resource actually exists, you are going to have to use the classes that derive from WebRequest/WebResponse. For HTTP and FTP resources, the HttpWebRequest/FtpWebRequest and HttpWebResponse/FtpWebResponse classes will work fine (as will WebClient), but if you have other schemes that you have to support, you will have to find specific providers for that scheme.

casperOne
+15  A: 

To check if an url is valid instead of using exceptions you can use the TryCreate method:

Uri result;
if (Uri.TryCreate("http://www.google.com", UriKind.RelativeOrAbsolute, out result)) 
{
    // the url is valid
}
Darin Dimitrov
thats more like it. thanks!
dev.e.loper
Now according to Uri.TryCreate url such as hht://www.gogole.com is valid. Even though the htt: is invalid scheme. Why is htt: okay?
dev.e.loper
htt: is perfectly valid scheme. A custom protocol could define this scheme.
Darin Dimitrov
i tried this with an invalid Uri like this /folder/{ht.com.m\\/sx.r:erp://°? and returns true but the out parameter throws exceptions in every property, i guess the only way to truly test if the string represent an Uri is if the Uri is absolute
Juan Zamudio
I think you should use "UriKind.Absolute". Also one need to consider that there is a difference between an "Uri" and "Url". I think you looking for the latter and this is not a great way to do it.
dr. evil
Maybe you are right, but then the contract for the method should be TryCreate(string, out Uri)) instead
Juan Zamudio
+5  A: 

Can use the static IsWellFormedUriString method:

bool isValid = Uri.IsWellFormedUriString(url, UriKind.Absolute);

http://msdn.microsoft.com/en-us/library/system.uri.iswellformeduristring.aspx

Pat
+6  A: 

Using Uri.TryCreate can have some problems with relative Uris, with string like this "/folder/{ht.com.m\/sx.r:erp://" TryCreate returns true, so i create this extension method using IsWellFormedUriString and TyrCreate, I'm not sure TryCreate is necessary, with my little tests i get the same results with or without TryCreate

public static bool IsUri(this string source) {
  if(!string.IsNullOrEmpty(source) && Uri.IsWellFormedUriString(source, UriKind.RelativeOrAbsolute)){
    Uri tempValue;
    return (Uri.TryCreate(source, UriKind.RelativeOrAbsolute, out tempValue));
  }
  return (false);
}

Example

address= "http://www.c$nbv.gob.mx"
if(address.IsUri()){
  //returns false
}
address= "http://www.cnbv.gob.mx"
if(address.IsUri()){
  //returns true
}
address= "http://www.cnbv.gob.mx:80"
if(address.IsUri()){
  //returns true
}
address= "/directory/path"
if(address.IsUri()){
  //returns true
}
address= "~re#l|ativ[ainco#recta\car:.\peta"
if(address.IsUri()){
  //returns false
}
Juan Zamudio