HtmlAgilityPack and Authentication

谁说胖子不能爱 提交于 2019-12-01 18:10:37
jessehouwing

HtmlWeb.Load has a number of overloads, these accept either an instance of NetworkCredential or you can pass in a username and password directly.

Name // Description 
Public method Load(String) //Gets an HTML document from an Internet resource.  
Public method Load(String, String) //Loads an HTML document from an Internet resource.  
Public method Load(String, String, WebProxy, NetworkCredential) //Loads an HTML document from an Internet resource.  
Public method Load(String, String, Int32, String, String) //Loads an HTML document from an Internet resource. 

You do not need to pass in a WebProxy instance, or you can pass in the system default one.

Alternatively you can wire up the HtmlWeb.PreRequest and setup the credentials for the request.

htmlWeb.PreRequest += (request) => {
    request.Credentials = new NetworkCredential(...);
    return true;
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!