0

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?

Nick Kahn
  • 19,652
  • 91
  • 275
  • 406
  • I believe you are doing it right. Here is a question which has already been asked: http://stackoverflow.com/questions/924679/c-how-can-i-check-if-a-url-exists-is-valid You can use the WebResponse to check your URL – reggie Jun 27 '11 at 17:45
  • Validate that the URL is valid uri syntax? Or validate that URL points to an actual valid location? – The Evil Greebo Jun 27 '11 at 17:46

2 Answers2

1

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

WebResponse webResponse = req.GetResponse();
Maxim
  • 7,268
  • 1
  • 32
  • 44
0

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.

treetey
  • 829
  • 1
  • 6
  • 16