J'ai une méthode pour obtenir les identifiants et xpath si une URL particulière est donnée. Comment puis-je transmettre le nom d'utilisateur et le mot de passe avec la demande afin de pouvoir récupérer une URL nécessitant un nom d'utilisateur et un mot de passe?
using HtmlAgilityPack;
_web = new HtmlWeb();
internal Dictionary<string, string> GetidsAndXPaths(string url)
{
var webidsAndXPaths = new Dictionary<string, string>();
var doc = _web.Load(url);
var nodes = doc.DocumentNode.SelectNodes("//*[@id]");
if (nodes == null) return webidsAndXPaths;
// code to get all the xpaths and ids
Devrais-je utiliser une requête Web pour obtenir le code source de la page, puis transférer ce fichier dans la méthode ci-dessus?
var wc = new WebClient();
wc.Credentials = new NetworkCredential("UserName", "Password");
wc.DownloadFile("http://somewebsite.com/page.aspx", @"C:\localfile.html");
HtmlWeb.Load
a un certain nombre de surcharges, celles-ci acceptant soit une instance de NetworkCredential
soit vous pouvez directement HtmlWeb.Load
un nom d'utilisateur et un mot de passe.
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.
Vous n'avez pas besoin de transmettre une instance WebProxy
, ou vous pouvez transmettre celle par défaut du système.
Vous pouvez également HtmlWeb.PreRequest
et configurer les informations d'identification de la demande.
htmlWeb.PreRequest += (request) => {
request.Credentials = new NetworkCredential(...);
return true;
};