HtmlAgilityPack HtmlDocument.Load 抛出异常“对象未设置为实例”
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
运行您的代码时出现的错误是:
它是由标准 .NET Framework 编码类引发的。这意味着该页面声明了 .NET 不支持的编码。我是这样修复的:
PS:我正在使用 Html Agility Pack 版本 1.3
The error I get when I run your code is:
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:
PS: I'm using the Html Agility Pack version 1.3
也许不是您需要的答案,但堆栈跟踪表明在您将响应流类型传递到页面的加载方法后发生了异常。
在 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.