HtmlAgilityPack HtmlDocument.Load throws exception "object not set to an instance"

c# html-agility-pack

Question

var uri = new Uri("http://store.scrapbook.com/cos-pad825.html?t12-13=cosmo%20cricket&date=20110309");
var request = (HttpWebRequest)WebRequest.Create(url);
var cookieContainer = new CookieContainer();

request.CookieContainer = cookieContainer;
request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5";
request.Method = "GET";
request.AllowAutoRedirect = true;
request.Timeout = 15000;

var response = (HttpWebResponse)request.GetResponse();
var page = new HtmlDocument();
var stream = response.GetResponseStream();
page.Load(stream);

Causes an error. on the Load(stream) call. Any ideas?

Accepted Answer

The error I get when I run your code is:

System.ArgumentException: 'ISO-8559-1' is not a supported encoding name.

It's thrown by the standard .NET Framework encoding classes. It means the page declares an encoding not supported by .NET. I fixed it like this:

var page = new HtmlDocument();
page.OptionReadEncoding = false;

PS: I'm using the Html Agility Pack version 1.3


Popular Answer

Maybe not a the answer you need, but the stack trace indicates the exception occurred after you past the response stream type to the page's load method.
It might be worth adding a TextReader in before the htmldocument assignment, and pass off the stream object to that. Then pass the textreader var to htmldoc's Load method.

Before you debug the source code for latest htmlagility, I think you should first edit your question to include the states of all types/props of interest, for clarity.




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