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.

2
  • You should first check for any similar questions before asking one on StackOverflow. Commented Jan 19, 2012 at 6: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_ Commented Jan 19, 2012 at 6:48

6 Answers 6

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...

Sign up to request clarification or add additional context in comments.

2 Comments

i am not looking url string is validation or not .i want to check each browse url status is =400 or 200
Ok, I will update my answer...
1

I think same question is answered already

Check this below link

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

Comments

0

Look into webresponse:

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

you can do something with that response I suppose.

Comments

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

Comments

0

You can do so by using webrequest and webresponse.

Have a look at this SO question.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.