// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; namespace E { public class MainClass { public static bool Test(Uri baseUri, string link) { try { Uri testUri = new Uri(link); return (baseUri.Scheme == testUri.Scheme) && (baseUri.Host == testUri.Host); } catch (UriFormatException ex) { try { Uri testUri = new Uri(baseUri, link); return true; } catch (UriFormatException ex2) { return false; } } } public static void Main(string[] args) { Uri rootUri = new Uri("http://exp.dk"); string[] links = { "/about.html", "http://exp.dk/foo.php", "bar.php", "http://dr.dk/artikler/12245.html", "javascript:alert('foobar');" }; foreach(String link in links) { Console.WriteLine(link + " " + Test(rootUri, link)); } } } }