Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

How to validate a url in C#.


Thanks in advance
Posted
Comments
Ziee-M 22-May-14 6:40am    
Hi, what do you mean by validate? maybe you are refering to checking if the string is in a url format?

Use Uri.IsWellFormedUriString to check whether the URL is in correct form.

For example,
C#
Uri.IsWellFormedUriString("http://www.google.com", UriKind.Absolute);

It'll return true as the URL is in correct form.

To check the existence of a URL create a WebRequest:

C#
WebRequest request = WebRequest.Create("http://www.google.com");
try
{
     request.GetResponse();
}
catch //If exception thrown then couldn't get response from address
{
     /// The URL is incorrect
     MessageBox.Show("The URL is incorrect");
}
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900