使用 C# 以编程方式返回 Javascript 管理的 cookie
我正在尝试以编程方式 ping 一个网站(通过控制台应用程序)并返回该网站使用的所有 cookie 的详细信息。
我使用的以下方法仅捕获通过标头请求管理的 cookie,并错过使用 Javascript 设置的 cookie:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse();
foreach (Cookie c in response.Cookies)
{
cookie.Add(c);
}
有人可以提供如何扩展以包括 javascript 配置的 cookie 的建议吗?
谢谢!
I'm trying to programmatically ping a website (through a console application) and return details of all the cookies being used by that site.
The following approach I'm using only captures those cookies managed through the header request and misses the ones set using Javascript:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = new CookieContainer();
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
request.Method = "GET";
response = (HttpWebResponse)request.GetResponse();
foreach (Cookie c in response.Cookies)
{
cookie.Add(c);
}
Can someone possibly provide suggestions to how this can be extended to include javascript configured cookies?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,不确定这会有多大帮助,但是...
您问是否有一种简单的方法来获取由客户端 JavaScript 动态创建的 cookie,答案是否定的,没有(除非我缺少一些东西)。
是否有更困难的方法,例如包装 .NET 浏览器控件,让 javascript 通过自动 Web 脚本执行,然后抓取 DOM...不过,对我来说这听起来不是一个好主意。
欢迎任何其他想法。
Well, not sure how much help this will be but...
You are asking if there is an easy way to get cookies that are created dynamically by client-side javascript and the answer is no, there isn't (unless I'm missing something).
Is there a harder way, maybe, like wrapping the .NET browser control, letting the javascript execute through automated web scripts and then scraping the DOM... Doesn't sound like a good idea to me though.
Any other thoughts welcome.
刚刚设法实现了接近我想要的目标(仍在测试解决方案,并且它似乎缺少一些来自第三方的跟踪 cookie!)。然而我所做的是使用 Selenium 和 Chrome 驱动程序可执行文件。它的作用是打开一个 chrome 实例,导航到 URL,并拉回所有动态生成的信息,然后可以使用 C# 来查询这些信息。 :)
Just managed to achieve something close to what I wanted (still testing the solution, and it seems to be missing some tracking cookies from 3rd parties!). However what I did was use Selenium, and the Chrome Driver executable. What it does is open up an instance of chrome, navigates to the URL, and pulls back all the dynamically generated information, which can then be interrogated using C#. :)