开发者

define a web request for the specified URL

开发者 https://www.devze.com 2023-03-16 04:17 出处:网络
what is the best way to validate a valid url and through error message? 开发者_开发百科i am using something like this:

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

精彩评论

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