开发者

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!


 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.


A standard way of checking the existence of a link is to use a HEAD request, which causes the remote server to send the headers for the requested object, but not the object itself. If you thus requested an object that is not on the server, the server gives you the normal 404 response, but if it does exist, you get a 200 response and no data after the headers. This way very little uninteresting data goes over the wire.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜