Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most efficient way for testing links

I'm currently developping an app that is going through all the files on a server and checking every single hrefs to check wether they are valid or not. Using a WebClient or a HttpWebRequest/HttpWebResponse is kinda overkilling the process because it downloads the whole page each time, which is useless, I only need to check if the link do not return 404.

What would be the most efficient way? Socket seems to be a good way of doing it, however I'm not quite sure how this works.

Thanks for sharing your expertise!

like image 507
Pierluc SS Avatar asked Apr 10 '26 05:04

Pierluc SS


1 Answers

The most efficient would be to send the HEAD verb to preserve bandwidth.

var request = WebRequest.Create("http://google.com/");
request.Method = "HEAD";
using (var response = (HttpWebResponse)request.GetResponse())
{
    if (response.StatusCode == HttpStatusCode.OK)
    {
        // 200 OK
    }
}
like image 171
Darin Dimitrov Avatar answered Apr 12 '26 19:04

Darin Dimitrov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!