在 Web 浏览器控件中抓取 Cookie - WP7 [英] Grabbing Cookies in Web Browser Control - WP7

查看:15
本文介绍了在 Web 浏览器控件中抓取 Cookie - WP7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了登录网站的某个部分,我的应用程序的用户需要他们的 cookie.为此,我需要抓取它并将其传递给 url.

In order to log into a certain part of a website the users of my application require their cookie. To do this I need to grab it and pass it to url.

有谁知道如何从浏览器控件中获取某个网站的cookie?

Does anyone know how to grab a certain websites cookie from the browser control?

我看到了这个方法 但不是很清楚.

I saw this method but wasn't quite clear.

谢谢,TP.

推荐答案

从 WP 7.1 Mango发布"开始,如果有人可以调用它,请参阅 Windows Phone 的网络浏览器控件概述.它最近更新了一点,事实证明他们实际上已经添加了一些对从 WebBrowser 检索 cookie 的支持.在页面底部,您会找到一个小链接 GetCookies(WebBrowser) 指向新类的描述:WebBrowserExtensions 使用这个非常方便的方法.是的,这个班级只有一个成员.这是一个扩展方法,我想不需要解释.

As of WP 7.1 Mango "release", if one may call it, please see the WebBrowser Control Overview for Windows Phone. It has been recently updated a little bit, and it turns out that they actually have added some support for cookie-retrieval from the WebBrowser. On the bottom of the page you will find a tiny link GetCookies(WebBrowser) pointing to description of a new class: WebBrowserExtensions with this very handy method. Yes, this class has only that one single member. It's an extension method, I suppose no explanations needed on that.

我没怎么玩过这种方法,但似乎这将允许您访问与 JS 技巧完全相同的东西:当前 URL 的 cookieset.它可能不允许设置任何内容,也不允许查看其他 URL 的 cookie.也许如果你努力使用 CookieContainer 你会收到,但我怀疑.

I have not played with this method much, but it seems that this will allow you to access the very same thing as the JS trick: the cookieset for the current URL. It probably will not allow to set anything, nor to peek cookies for other URLs. Maybe if you play hard with the CookieContainer you will receive, but I doubt.

在 7.0 版本中,我一直在努力为我的应用实现cookie 透明度".长话短说,我的应用程序正在执行一些后台 HTTP 请求,并且还有一个 WebBrowser 来显示一些在线内容——如果两个连接源都向服务器发送相同的 cookie,那就太棒了".猜猜看,我的应用程序必须发出第一个请求,然后让浏览器导航.有了这样的要求,几乎没有办法实现 cookie 的一致性 - 呸,即使使用当前新的光荣的 GetCookie 方法,我想这也太难了.所以,就这一点而言 - 这是可能的,但需要使用一些隐藏的 API,这些 API 在手机上公开存在,但隐藏在 SDK 中.API 在(公共)类 System.Net.Browser.WebRequestCreator 中可用,免费提供.怪癖是:在 SDK 中,这个类有一个公共静态属性IWebRequestCreate ClientHttp"和一个方法Create",你可以用它来工厂"你的原始 http"连接 - 如果你不想使用 WebClient一些理由.在手机和模拟器上,有一个第二个公共静态属性,名为"IWebRequestCreate BrowserHttp",很容易被反射返回:

On the 7.0 release, I've been struggling quite hard to achieve "cookie transparency" for my App. Long story short, my app was doing some background HTTP requests, and also had a WebBrowser to show some online content -- and "it would be great" if both sources of connections would emit the same cookies to the server.. And guess what, my application had to make the first request, then let the browser navigate. With such requirements, there was virtually is no way to achieve consistency of the cookies - bah, even with the current new and glorious GetCookie method, I suppose it would be damn hard. So, to the point - it was possible, but needed to use some hidden API, that is present publicitly on the Phone, but is hidden in the SDK. The API is available the (public) class System.Net.Browser.WebRequestCreator, freely available. The quirk is: in the SDK this class has a single public static property "IWebRequestCreate ClientHttp" with a method "Create" that you can use to "factory" your "raw http" connections - in case you dont want to use the WebClient for some reason. On the phone, and on the emulator, there is a second public static property called "IWebRequestCreate BrowserHttp", easily returned by Reflection:

PropertyInfo brwhttp = typeof(System.Net.Browser.WebRequestCreator)
    .GetProperty("BrowserHttp")

使用此属性,您将能够获得 WebBrowser 内部使用的 IWebRequestCreate 的特殊"内部实例.通过使用此类打开后台 HTTP 请求,您将自动设置 cookie,就好像它们是由 WebBrowser 控件创建/发送的一样,但反过来 - 您将不能修改 http 标头,用户提供 http 用户身份验证,也不做一些低级的事情 - 因为所有这些设置都将与为当前系统用户实例"存储的 WebBrowser 数据同步,如果我被允许这样调用它单用户电话设备呵呵.连接和 WebBrowser 之间的互操作双向 - 如果您的 HTTP 连接(使用隐藏属性"创建)接收任何设置/cookies/等 - 那么 WebBrowser 会立即注意到它们并更新自己的缓存.双方都没有 cookie/session 丢失!

with this property, you will be able to obtain a "special" internal instance of IWebRequestCreate that is used internally by the WebBrowser. By opening your background HTTP requests with this class, you will get your cookies automatically set as if they were created/sent by the WebBrowser control, but in turn - you will NOT be able to modify http headers, userprovide http user authentication and neither do a few lowlevel things - because all that settings will be synced with the WebBrowser's data stored for current 'system user instance', if I'm allowed to call it as such on the single-user Phone device heh. The interoperation between connections and the WebBrowser works both ways - if your HTTP connection (created with use of the 'hidden property') receives any settings/cookies/etc -- then the WebBrowser will instantly notice them and update its own cache. No cookie/session loss on neither of the sides!

如果您需要在第一次 WebBrowser 导航后为您的后续连接被动获取 cookie - 请使用 GetCookie 或 JS 方式.但是,如果您需要首先使用您的代码,然后将 authz 传递给 WebBrowser - 您可能需要深入挖掘并使用上述内容..它已被隐藏,因此请先求助于其他方式!

If you need to passively get cookies for your subsequent connections after some first WebBrowser navigation - please use the GetCookie or the JS way. But if you need your code to be first, and then pass authz to the WebBrowser -- you will probably have to dig deeper and use the above.. It's been hidden, so please resort to the other means first!

..别问我是怎么找到它的,或者花了多长时间:P玩得开心

..and don't ask me how did I found it or how much time it took :P have a nice fun with it

//edit: 我刚刚发现,BrowserHttp 属性是 Silverlight 访问浏览器连接工厂的正常方式,请参阅 BrowserHttp.看来只能隐藏在为WP7平台编写的'miniSilverlight'中了!

//edit: I've just found out, that the BrowserHttp property is a normal Silverlight's way to access the Browser's connection factory, please see BrowserHttp. It seems that it is only has been hidden in the 'miniSilverlight' written for the WP7 platform!

这篇关于在 Web 浏览器控件中抓取 Cookie - WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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