C# HtmlAgilityPack TypeAccessException when loading page from Url

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

Question

I am trying to develop a windows phone app which parses an HTML page (retrieved over the web) into a HTMLDocument variable and use that to inspect the page, retrieve values etc etc. Even when trying to connect to pages like the BBC News Home page I am getting an error.

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()

This page shouldn't need credentials so I am very confused as to why I am encountering this error. Here is the code.

private async Task GetHtmlDocument(string url)
{
    Debug.WriteLine("This is a line");
    try
    {
        HtmlWeb web = new HtmlWeb();
        HtmlDocument rootDocument = await web.LoadFromWebAsync(url);
    }
    catch (Exception e)
    {
        Debug.WriteLine("Exception: " + e);
    }
}

Accepted Answer

I am using Windows Phone 8.1 which I probably needed to mention in the question. The following is working.

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 (...) { ... }
}

Popular Answer

I pasrse Html by html-agility-pack like this:

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));
}

and then

string text;

HtmlAgilityPack.HtmlNode node = _doc.DocumentNode.SelectSingleNode(Xpath);
if (node != null) text = node.InnerText;


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why