在 HttpTests 中,有一种方法可以使用
request.Credentials = CredentialCache.DefaultCredentials;
Html Agility Pack 中有类似的东西吗?我想测试我的本地主机项目,但它收到:
HTTP 错误 401.2 - 未经授权 您无权查看此页面
在 HttpTests 中,有一种方法可以使用
request.Credentials = CredentialCache.DefaultCredentials;
Html Agility Pack 中有类似的东西吗?我想测试我的本地主机项目,但它收到:
HTTP 错误 401.2 - 未经授权 您无权查看此页面
我找到了 Jon Gallant 的博客:http: //blog.jongallant.com/2012/07/htmlagilitypack-windows-authentication.html#.UJEQam8xol8
创建一个新的 HtmlWeb 实例,创建一个将 UseDefaultCredentials 设置为 true 的新 WebProxy,在 webload 上创建一个名为 document 的新变量,将 url 设置为 GET 请求,插入默认凭据并获取应用程序的系统凭据。
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://localhost:2120", "GET", proxy,
CredentialCache.DefaultNetworkCredentials);
var linksOnPage = from lnks in document.DocumentNode.Descendants()
where lnks.Name == "a" &&
lnks.Attributes["href"] != null &&
lnks.InnerText.Trim().Length > 0
select new
{
Url = lnks.Attributes["href"].Value,
Text = lnks.InnerText
};
linksOnPage.All(t => { Console.WriteLine(t.Text + " : " + t.Url); return true; });
Is there a way to authenticate a windows authenticated user in Html Agility Pack?
No,
What is exactly the Html Agility Pack (HAP)?
This is an agile HTML parser that builds a read/write DOM and supports plain XPATH or XSLT.
It is a .NET code library that allows you to parse "out of the web" HTML files. The parser is very tolerant with "real world" malformed HTML. The object model is very similar to what proposes System.Xml, but for HTML documents (or streams).
Html Agility Pack now supports Linq to Objects (via a LINQ to Xml Like interface).
Sample applications:
Page fixing or generation : You can fix a page the way you want, modify the DOM, add nodes, copy nodes, well... you name it.
Web scanners : You can easily get to img/src or a/hrefs with a bunch XPATH queries.
Web scrapers : You can easily scrap any existing web page into an RSS feed for example, with just an XSLT file serving as the binding. An example of this is provided.