HtmlAgilityPack HtmlDocument.Load 抛出异常“对象未设置为实例”

发布于 2024-10-23 14:23:13 字数 658 浏览 7 评论 0原文

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

导致错误。在 Load(stream) 调用上。有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦开始←不甜 2024-10-30 14:23:13

运行您的代码时出现的错误是:

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

它是由标准 .NET Framework 编码类引发的。这意味着该页面声明了 .NET 不支持的编码。我是这样修复的:

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

PS:我正在使用 Html Agility Pack 版本 1.3

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

饭团 2024-10-30 14:23:13

也许不是您需要的答案,但堆栈跟踪表明在您将响应流类型传递到页面的加载方法后发生了异常。
在 htmldocument 分配之前添加一个 TextReader 可能值得,并将流对象传递给它。然后将 textreader var 传递给 htmldoc 的 Load 方法。

在调试最新 htmlagility 的源代码之前,为了清楚起见,我认为您应该首先编辑您的问题以包括所有感兴趣的类型/属性的状态。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文