How to load a webpage that uses Windows Authentication with HtmlAgilityPack, WebProxy and UseDefaultCredentials

It wasn’t obvious to me how to load an Html doc that requires Windows Authentication. Here’s a quick code snippet that hopefully saves you some time.

The trick is to set UseDefaultCredentials on the WebProxy object that is passed to the Load method.

var web = new HtmlWeb();
web.PreRequest = delegate(HttpWebRequest webRequest)
{
    webRequest.Timeout = 1200000;
    return true;
};
var proxy = new WebProxy() { UseDefaultCredentials = true };
var doc = web.Load("http://[windowsauthurl]", "GET", proxy, CredentialCache.DefaultNetworkCredentials);