Nick Kahn
Nick Kahn

Reputation: 20078

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?

Upvotes: 0

Views: 162

Answers (2)

treetey
treetey

Reputation: 829

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.

Upvotes: 0

Maxim
Maxim

Reputation: 7348

If you want to see if you get a response from that URL you have to -

WebResponse webResponse = req.GetResponse();

Upvotes: 1

Related Questions