what is the best way to validate a valid url and through error message?
开发者_开发百科i am using something like this:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
i am doing try and catch to catch the error message
is that enough or can be do better then that?
If you want to see if you get a response from that URL you have to -
WebResponse webResponse = req.GetResponse();
You can use the regular expressions (System.Text.RegularExpressions namespace) to test for valid URLs:
var urlTester = new Regex( @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" );
bool isValidUrl = urlTester.IsMatch( url );
Also ask google for other Regex URL patterns if it's needed.
精彩评论