在拼抢Web浏览器控件饼干 - WP7 [英] Grabbing Cookies in Web Browser Control - WP7

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

问题描述

为了登录到一个网站的某个部分我的应用程序的用户需要将他们的Cookie。要做到这一点,我需要抓住它,并把它传递给URL。

有谁知道如何抓住某网站从浏览器控件饼干吗?

我看了<一个href=\"http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/1455b8a9-b6f3-47f2-853c-f5eceb812725\"相对=nofollow>这个方法,但不太清楚。

谢谢,TP。


解决方案

作为WP 7.1芒果释放,如果人们可以调用它,请参阅的 WebBrowser控件概述Windows Phone的。它最近已经更新了一点点,而且事实证明,他们其实已经添加了来自web浏览器的cookie检索一些支持。在页面的底部,你会发现一个小小的链接<一个href=\"http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.webbrowserextensions.getcookies%28v=vs.92%29.aspx\"相对=nofollow>的getCookies(web浏览器)指向一类新的描述:<一href=\"http://msdn.microsoft.com/en-us/library/microsoft.phone.controls.webbrowserextensions_members%28v=VS.92%29.aspx\"相对=nofollow> WebBrowserExtensions 与此非常方便的方法。是的,这个类只有一个单一的成员。这是一个扩展方法,我想没有必要上的解释。

我没有用这种方法出场不多,但似乎这将允许您访问同样的事情作为JS绝招:cookieset当前URL。它可能不会允许任何设置,也没有偷看其他网址的饼干。也许如果你用的CookieContainer努力打球,你会收到,但我怀疑。

在7.0版本中,我一直在挣扎挺难实现饼干透明度我的应用程序。长话短说,我的应用程序在做一些背景的HTTP请求,并且也有一个web浏览器来显示一些在线内容 - 而这将是巨大的,如果连接两个来源会发出同样的Cookie到服务器..你猜是什么,我的应用程序的中有的做出的第一个请求,然后让浏览器浏览。有了这样的需求,基本上是没有办法实现的cookie一致性 - 呸,即使在目前新的辉煌的getCookie方法,我想这将是该死的努力。所以,给点 - 这是可能的,但使用一些隐藏的API需要的,也就是present publicitly的手机,但隐藏在SDK。 API是可用的(公共)类的 System.Net.Browser.WebRequestCreator ,免费提供。的怪癖是:在SDK这个类有一个公共静态属性IWebRequestCreate ClientHttp与方法创建,你可以用它来工厂你的原始HTTP连接 - 如果你不想使用的Web客户端某些原因。在手机上,并在模拟器上,有一个第二公共静态属性名为IWebRequestCreate BrowserHttp后,通过反射返回轻松:

 的PropertyInfo brwhttp = typeof运算(System.Net.Browser.WebRequestCreator)
    .GetProperty(BrowserHttp)

与这个属性,你将能够获得一个特殊的内部IWebRequestCreate的由web浏览器内部使用实例。通过与此类打开你的背景的HTTP请求,你会获取你的cookies自动设置为如果他们创建/ WebBrowser控件发送,但反过来 - 你的无法修改的HTTP报头,userprovide HTTP用户认证和没有做一些低级的东西 - 因为所有的设置将被保存当前的系统用户实例的web浏览器的数据同步,如果我允许它呼吁这样的单用户Phone设备嘿嘿。连接和WebBrowser作品之间的互操作的左右逢源 - 如果你的HTTP连接(与使用隐藏属性的创建)接收任何设置/饼干/等 - 那么web浏览器会立即通知他们,更新自己的缓存。没有上既没有双方的Cookie /会话丢失!

如果你需要一些先web浏览器导航后被动地获取饼干为您的后续连接 - 请使用的getCookie或者JS方式。
但是,如果你需要你的code是第一,然后再传递到AuthZ的web浏览器 - 你可能必须深入挖掘和使用上述..它被隐藏,因此请首先诉诸其他手段

..不要问我我怎么发现的,还是多少时间了:P
有一个很好的它的乐趣。

//编辑:我刚刚发现,该BrowserHttp属性是一个正常的Silverlight的方式来访问浏览器的连接工厂,请参阅<一个href=\"http://msdn.microsoft.com/en-us/library/system.net.browser.webrequestcreator.browserhttp%28v=VS.95%29.aspx\"相对=nofollow> BrowserHttp 。它似乎只是一直隐藏在为WP7平台编写了miniSilverlight'!

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.

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

I saw this method but wasn't quite clear.

Thanks, TP.

解决方案

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.

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.

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

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!

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!

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

//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浏览器控件饼干 - WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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