0

Possible Duplicate:
C# How can I check if a URL exists/is valid?

Work on C# vs2010.

Want to check url are valid or not .Suppose I have three url like www.google.com,www.ggg.com,www.gef.com .

When I brows on www.google.com =if page available then I want a response , this page is valid and url is valid.

When I brows on www.ggg.com or www.gef.com =if page not available then I want a response ,this page is not valid and url is not valid.

Is it possible ?Is there any idea ,or suggestion to solve this problem? Thanks in advance ,if have any query plz ask.

Community
  • 1
  • 1
shamim
  • 6,640
  • 20
  • 85
  • 151
  • You should first check for any similar questions before asking one on StackOverflow. – Bibhu Jan 19 '12 at 06:31
  • I added an answer, but if you want to customize it, I can help you. Just add comment( your needs ) to under my answer. And good luck_ – Lost_In_Library Jan 19 '12 at 06:48

6 Answers6

2

.NET: Check URL's response status code?

First answer will return "url status". Then check the return status with if condition == "400" .. etc.

An example;


List< string > urls = new List< string >
urls.add("www.google.com");
urls.add("www.ggg.com");

foreach(var url in urls) { //cast string to HttpResponse will need here... if( GetHeaders(url).ToString() == "400" ) MessageBox.Show(url + " status code is 400"); }

Something like that...

Community
  • 1
  • 1
Lost_In_Library
  • 3,265
  • 6
  • 38
  • 70
1

I think same question is answered already

Check this below link

C# How can I check if a URL exists/is valid?

Community
  • 1
  • 1
Manjunath K Mayya
  • 1,078
  • 1
  • 11
  • 20
0

Look into webresponse:

http://msdn.microsoft.com/en-us/library/system.net.webresponse.aspx

you can do something with that response I suppose.

Jason Dam
  • 390
  • 1
  • 8
0

You can validate url with Tegular Expression; Please have a look on below links: http://madskristensen.net/post/Validate-a-URL-using-regular-expressions.aspx http://www.webpronews.com/validating-a-url-with-regular-expressions-2006-10

Saurabh
  • 5,661
  • 2
  • 26
  • 32
0

You can do so by using webrequest and webresponse.

Have a look at this SO question.

Community
  • 1
  • 1
Bibhu
  • 4,053
  • 4
  • 33
  • 63
0

Please take a look on WebClient and Uri classes.

With WebClient class (from System.Net namespace) you can check whether resource referred by Url is available:

bool resourceAvailable = false;

WebClient webClient = new WebClient();

try
{
    string pageContent = webClient.DownloadString("http://www.someResource.com");
    resourceAvailable = !String.IsNullOrEmpty(pageContent);
}
catch (WebException) { }

// then you can perform actions depending on value of resourceAvailable flag (variable)

With Uri class you can check whether url address is properly formatted:

Uri.IsWellFormedUriString("http://www.someAddress.com", UriKind.Absolute); // will return true
Uri.IsWellFormedUriString("not an uri", UriKind.Absolute); // will return false

You can also use UriBuilder class to complete fromatting of URLs.

Andrii Kalytiiuk
  • 1,501
  • 14
  • 26