开发者

define a web request for the specified URL

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜