C # HtmlAgilityPack TypeAccessException lors du chargement de la page à partir de l'URL

c# exception html-agility-pack request windows-mobile

Question

J'essaie de développer une application Windows Phone qui analyse une page HTML (récupérée sur le Web) en une variable HTMLDocument et l'utilise pour inspecter la page, récupérer des valeurs, etc. Même lors d'une tentative de connexion à des pages telles que la page d'accueil BBC News. Je reçois une erreur.

System.TypeAccessException: Attempt by security transparent method 'HtmlAgilityPack.HtmlWeb.LoadFromWebAsync(System.Uri, System.Text.Encoding, System.Net.NetworkCredential)' to access security critical type 'System.Net.NetworkCredential' failed.
   at HtmlAgilityPack.HtmlWeb.LoadFromWebAsync(Uri uri, Encoding encoding, NetworkCredential credentials)
   at HtmlAgilityPack.HtmlWeb.<LoadFromWebAsync>d__0.MoveNext()

Cette page ne devrait pas nécessiter d'informations d'identification. Je ne comprends donc pas très bien pourquoi je rencontre cette erreur. Voici le code.

System.TypeAccessException: Attempt by security transparent method 'HtmlAgilityPack.HtmlWeb.LoadFromWebAsync(System.Uri, System.Text.Encoding, System.Net.NetworkCredential)' to access security critical type 'System.Net.NetworkCredential' failed.
   at HtmlAgilityPack.HtmlWeb.LoadFromWebAsync(Uri uri, Encoding encoding, NetworkCredential credentials)
   at HtmlAgilityPack.HtmlWeb.<LoadFromWebAsync>d__0.MoveNext()

Réponse acceptée

J'utilise Windows Phone 8.1, ce dont j'avais probablement besoin de mentionner dans la question. Ce qui suit fonctionne.

private async Task GetHtmlDocument(string url)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    request.Credentials = new LoginCredentials().Credentials;

    try
    {
        WebResponse myResponse = await request.GetResponseAsync();
        HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
        htmlDoc.OptionFixNestedTags = true;
        htmlDoc.Load(myResponse.GetResponseStream());
    } catch (...) { ... }
}

Réponse populaire

Je pasrse Html par html-agility-pack comme ceci:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

//cookie if you need
request.CookieContainer = cookie;
//WebRequestMethods.Http.Get
//or WebRequestMethods.Http.Post
request.Method = method;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Language",  "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
request.ServicePoint.Expect100Continue = false;
request.Timeout = 100000;
request.ContentType = "application/x-www-form-urlencoded";

using (WebResponse myResponse = request.GetResponse())
{   
    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.OptionFixNestedTags = true;
    htmlDoc.Load(myResponse.GetResponseStream(), Encoding.GetEncoding(1251));
}

et alors

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);

//cookie if you need
request.CookieContainer = cookie;
//WebRequestMethods.Http.Get
//or WebRequestMethods.Http.Post
request.Method = method;
request.UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36";
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Language",  "ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4");
request.ServicePoint.Expect100Continue = false;
request.Timeout = 100000;
request.ContentType = "application/x-www-form-urlencoded";

using (WebResponse myResponse = request.GetResponse())
{   
    HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
    htmlDoc.OptionFixNestedTags = true;
    htmlDoc.Load(myResponse.GetResponseStream(), Encoding.GetEncoding(1251));
}



Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi
Sous licence: CC-BY-SA with attribution
Non affilié à Stack Overflow
Est-ce KB légal? Oui, apprenez pourquoi