16

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?

Community
  • 1
  • 1
dev.e.loper
  • 34,180
  • 71
  • 151
  • 237

4 Answers4

32

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
  • 960,118
  • 257
  • 3,196
  • 2,876
  • 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 Mar 31 '09 at 18:19
  • 1
    htt: is perfectly valid scheme. A custom protocol could define this scheme. – Darin Dimitrov Apr 01 '09 at 07:22
  • 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 May 13 '09 at 05:48
  • 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 May 13 '09 at 17:35
  • Maybe you are right, but then the contract for the method should be TryCreate(string, out Uri)) instead – Juan Zamudio May 13 '09 at 18:18
  • I recommend use `UriKind.Absolute` instead `UriKind.RelativeOrAbsolute`: ` string uri = "zzzzz http://www.google.com.br/kkkkkkkkkkkk"; Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out result)` – Kakashi Sep 07 '11 at 20:03
  • I really doesn't understand why this bad answer has 29 upvotes. the Relative just checks for anything, and the Absolute just checks for the "http://" starting word, so RelaiveOrAbsolute just checks fon a not-empty string, and thats all, you can put a single character and that will be valid, so this is not a way to check it, is poor than a regex. very, very bad answer. – ElektroStudios Sep 01 '14 at 03:03
  • One more quirk to keep in mind is the change in System.URI ctor behavior in .NET version 4.5. See [here](https://blogs.msdn.microsoft.com/dotnet/2012/10/17/net-framework-4-5-off-to-a-great-start/). Essentially: _In the .NET Framework 4.5, an invalid mailto: URL throws an exception in the Uri class constructor, to comply with the URI RFC_ (while it doesn't in .NET Framework 4 and below). – Santosh Jan 30 '18 at 05:58
13

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
  • 329
  • 9
  • 32
9

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
  • 5,220
  • 1
  • 36
  • 53
0

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.

Community
  • 1
  • 1
casperOne
  • 70,959
  • 17
  • 175
  • 239