HtmlAgilityPack.HtmlDocument Cookie [英] HtmlAgilityPack.HtmlDocument Cookies

查看:1109
本文介绍了HtmlAgilityPack.HtmlDocument Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



System.Windows.Forms.HtmlDocument 执行这些脚本并且可以通过其 Cookie 属性检索cookie集合(如 document.cookie = etc ... )。



我假设 HtmlAgilityPack.HtmlDocument 不这样做(执行)。我想知道是否有一个简单的方法来模拟 System.Windows.Forms.HtmlDocument 功能(cookies部分)。



任何人?

解决方案

Cookie 和 HtmlAgilityPack ,或者只是创建自定义请求(例如,设置 User-Agent 属性等)我所做的:




  • 创建封装请求/响应的类。让我们调用这个类 WebQuery

  • 有一个私人的 CookieCollection strong>)属性

  • 在手动创建请求的类中创建一个方法。签名可以是:



...

  public HtmlAgilityPack.HtmlDocument GetSource(string url); 

我们需要在此方法中做什么?

好吧,使用HttpWebRequest和 HttpWebResponse ,手动生成http请求(有几个例子,如何在互联网上这样做) ,使用接收流的构造函数创建 HtmlDocument 类的实例。



  httpResponse.GetResponseStream(); 

如果使用 HttpWebRequest 进行查询, CookieContainer 属性指向您在每次访问新网页之前声明的变量,这样您访问的网站设置的所有Cookie都将正确存储在您在 WebQuery 类中声明的 CookieContainer 变量,计入您仅使用一个 WebQuery 类。



希望你能找到有用的解释。使用这个,你可以做任何你想要的,无论是否支持HtmlAgilityPack 。


This pertains to cookies set inside a script (maybe inside a script tag).

System.Windows.Forms.HtmlDocument executes those scripts and the cookies set (like document.cookie=etc...) can be retrieved through its Cookies property.

I assume HtmlAgilityPack.HtmlDocument doesn't do this (execution). I wonder if there is an easy way to emulate the System.Windows.Forms.HtmlDocument capabilities (the cookies part).

Anyone?

解决方案

When I need to use Cookies and HtmlAgilityPack together, or just create custom requests (for example, set the User-Agent property, etc), here is what I do:

  • Create a class that encapsulates the request/response. Let's call this class WebQuery
  • Have a private CookieCollection (in your case public) property inside that class
  • Create a method inside the class that does manually the request. The signature could be:

...

public HtmlAgilityPack.HtmlDocument GetSource(string url);

What do we need to do inside this method?

Well, using HttpWebRequest and HttpWebResponse, generate the http request manually (there are several examples of how to do this on Internet), create an instance of a HtmlDocument class using the constructor that receives an stream.

What stream do we have to use? Well, the one returned by:

httpResponse.GetResponseStream();

If you use HttpWebRequest to make the query, you can easily set the CookieContainer property of it to the variable you declared before everytime you access a new page, and that way all cookies set by the sites you access will be properly stored in the CookieContainer variable you declared in your WebQuery class, taking in count you're using only one instance of the WebQuery class.

Hope you find useful this explanation. Take in count that using this, you can do whatever you want, no matter if HtmlAgilityPack supports it or not.

这篇关于HtmlAgilityPack.HtmlDocument Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆