Search code examples
c#uwphtml-agility-packaccess-violation

System.AccessViolationException is thrown when adding an "Accepted-Language" to a HtmlWeb pre-request's headers


When debugging my UWP-application, it throws a System.AccessViolationException with the message 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.' and the stack trace is null. The exception is thrown when trying to add an "Accept-Language" ("en-US") to the HtmlWeb object's pre-request header (see the picture and code below). Running the same code using xUnit works fine. Does someone recognize the problem?

Picture of thrown exception: https://i.sstatic.net/17gHB.png

public static HtmlNode GetHtmlNode(string url, string requestLanguage)
{
    var htmlWeb = new HtmlWeb();
    htmlWeb.PreRequest += (request) =>
    {
        // This line of code throws the exception (see the picture as well)
        request.Headers.Add("Accept-Language", requestLanguage);
        return true;
    };
    return htmlWeb.Load(url).DocumentNode;
}

Solution

  • I solved the issue by not using HtmlWeb at all, as shown below:

    public static HtmlNode GetHtmlNode(string url, string requestLanguageCode)
    {
        try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.UserAgent = "Mozilla";
            request.Accept = "Accept: text/html";
            request.Headers.Add("Accept-Language: " + requestLanguageCode);
    
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            var stream = response.GetResponseStream();
    
            using (var reader = new StreamReader(stream))
            {
                string html = reader.ReadToEnd();
                var doc = new HtmlDocument();
                doc.LoadHtml(html);
                return doc.DocumentNode;
            }
        }
        catch (WebException)
        {
            return null;
        }
    }