Search code examples
c#httpwebrequesthtml

How to get the webpage source code using C#


I know about the WebRequest and the WebResponse objects. The problem is that I do not really want to get the source code of the webpage, I only want to check to see if the link exists or not. The thing is, if I use the GetResponse method, it goes an pull the entire source code of the site.

I am creating a broken link checker with many links. It takes quite a while to check them all. If there a way to to get MINIMAL information from a weblink? Only enough information to see if the link is valid or broken (not the entire source code).

An answer (BESIDES USING ASYNCHRONOUS TRANSFER) would be greatly appreciated!


Solution

  •  WebRequest request = HttpWebRequest.Create("http://www.foo.com/");
     request.Method = "HEAD"; // Just get the document headers, not the data.
    

    HEAD is similar to GET, only that instead of getting the file contents, we get just the headers.