string url = "";
// TEST CASES
url = "http://newsroom.lds.org/ldsnewsroom/eng/news-releases-stories/local-churches-teach-how-to-plan-for-disasters"; //Prints "BROKEN", although this is getting re-written to good url below.
//url = "http://beta-newsroom.lds.org/article/local-churches-teach-how-to-plan-for-disasters"; // Prints "GOOD"
//url = "http://"; //Prints "BROKEN"
//url = "google.com"; //Prints "BROKEN" althought this should be good.
//url = "www.google.com"; //Prints "BROKEN" althought this should be good.
//url = "http://www.google.com"; //Prints "GOOD"
try
{
if (url != "")
{
WebRequest Irequest = WebRequest.Create(url);
WebResponse Iresponse = Irequest.GetResponse();
if (Iresponse != null)
{
_txbl.Text = "GOOD";
}
}
}
catch (Exception ex)
{
_txbl.Text = "BROKEN";
}
how to validate a URL in c#
Hi, I need to write a tool that will report broken URL's. The URL should only reports broken if the user see's a 404 Error in the browser. I believe there might be tricks to handle web servers that do URL re-writing. Here's what I have. As you can see only some URL validate incorrectly. Thanks you for any help.
Sam HobbsPosted Sep 30, 2010, 6:36 PM
at the end of each line. I am not sure if it helps to use
George LovelessPosted Sep 30, 2010, 10:39 AM
I'm not sure I know what you mean. I don't want to check the url with a regular expression it that's what you mean. My code test cases might make my question more clear. I don't know why my code appears jumbled on this forum... frustrating.
Gomathi PalaniswamyPosted Sep 30, 2010, 2:03 AM
take the url string and test what are the char you need and how to it should be.