0

I am developing a tool for validation of links in url entered. suppose i have entered a url (e.g http://www-review-k6.thinkcentral.com/content/hsp/science/hspscience/na/gr3/se_9780153722271_/content/nlsg3_006.html ) in textbox1 and i want to check whether the contents of all the links exists on remote server or not. finally i want a log file for the broken links.

1
  • Hey user564931, welcome to the community. Be sure to vote up any answers you found helpful (like Scott's). Jan 6, 2011 at 5:40

6 Answers 6

3

You can use HttpWebRequest.

Note four things

1) The webRequest will throw exception if the link doesn't exist

2) You may like to disable auto redirect

3) You may also like to check if it's a valid url. If not, it will throw UriFormatException.

UPDATED

4) Per Paige suggested , Use "Head" in request.Method so that it won't download the whole remote file

    static bool UrlExists(string url)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Method = "HEAD";
            request.AllowAutoRedirect = false;
            request.GetResponse();
        }
        catch (UriFormatException)
        {
            // Invalid Url
            return false;
        }
        catch (WebException ex)
        {
            // Valid Url but not exists
            HttpWebResponse webResponse = (HttpWebResponse)ex.Response;
            if (webResponse.StatusCode == HttpStatusCode.NotFound)
            {
                return false;
            }
        }
        return true;
    }
2
  • Hmm... Perhaps, I shouldn't check the StatusCode == NotFound since it's WebException anyway Jan 6, 2011 at 5:21
  • 2
    I would also recommend only sending HEAD request so you do not end up downloading the entire remote resource file. In this case, add request.Method = "HEAD" just before the request.GetResponse() line above.
    – Paige Cook
    Jan 31, 2011 at 15:40
1

Use the HttpWebResponse class:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("http://www.gooogle.com/");            
HttpWebResponse response = (HttpWebResponse)webRequest.GetResponse();

if (response.StatusCode == HttpStatusCode.NotFound)
 {
     // do something
 }
1
  • Thanks a lot Scott for your valuable support Jan 6, 2011 at 5:02
1
bool LinkExist(string link)
{
   HttpWebRequest webRequest = (HttpWebRequest) webRequest.Create(link);
   HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
   return !(webResponse.StatusCode != HttpStatusCode.NotFound);
}
1

Use an HTTP HEAD request as explained in this article: http://www.eggheadcafe.com/tutorials/aspnet/2c13cafc-be1c-4dd8-9129-f82f59991517/the-lowly-http-head-reque.aspx

0

Make a HTTP request to the URL and see if you get a 404 response. If so then it does not exist.

Do you need a code example?

0

If your goal is robust validation of page source, consider usign a tool that is already written, like the W3C Link Checker. It can be run as a command-line program that handles finding links, pictures, css, etc and checking them for validity. It can also recursively check an entire web-site.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.