使用 JavaFX 的 WebEngine/WebView 设置 cookie [英] Setting a cookie using JavaFX's WebEngine/WebView

查看:69
本文介绍了使用 JavaFX 的 WebEngine/WebView 设置 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到任何使用 WebEngine/WebView 在 JavaFX 中.API 不知道如何获取类似 HttpRequest 的对象来修改标头(这是我在应用程序中用于 XML-RPC 的内容)或任何类型的 cookie 管理器.

I cannot seem to find any way to set a cookie programatically using WebEngine / WebView in JavaFX. The API doesn't give any idea as to how to obtain an HttpRequest-like object to modify the headers (which is what I use in the app for XML-RPC), or any sort of cookie manager.

此页面上的问题似乎也没有涉及该问题 - 有 但它只是在小程序中禁用cookie来修复错误,顺便说一句,我的应用程序在桌面上.

No questions on this page seem to touch on the issue either - there is this but it just disables cookies when in applet to fix a bug, my app is on desktop btw.

我想象的唯一方法是请求第一页(这需要一个带有 sessionID 的 cookie 才能正确加载),获取访问被拒绝"样式的消息,在页面上下文中执行一些 javascript 设置cookie,然后刷新.不过,这种解决方案会带来糟糕的用户体验.

The only way I image I could do it is by requesting the first page (which requires a cookie with a sessionID to load properly), getting an "access denied"-style message, executing some javascript in the page context which sets the cookie and then refreshing. This solution would be a horrible user experience though.

如何使用 WebEngine 设置 cookie?

How do I set a cookie using WebEngine?

更新:从上面链接的问题中得到线索,我尝试挖掘一些使用 CookieManager 和相关 API.我找到了这个代码,然后我尝试将其合并到我的应用程序,结果很奇怪;

Update: Taking a clue from a question linked above, I tried digging around for some examples of using CookieManager and related APIs. I found this code, which I then tried to incorporate into my app, with weird results;

MyCookieStore cookie_store = new MyCookieStore();
CookieManager cookie_manager = new CookieManager(cookie_store, new MyCookiePolicy());
CookieHandler.setDefault(cookie_manager);
WebView wv = new WebView();

现在假设我们这样做:

String url = "http://www.google.com/";
wv.getEngine.go(url);

发出此请求后在 Eclipse 中的调试显示 cookie 存储映射包含一个 cookie:

Debugging in Eclipse after this request has been made shows that the cookie store map holds a cookie:

<代码> {http://www.google.com/= [NID = 67 = XWOQNK5VeRGEIEovNQhKsQZ5-laDaFXkzHci_uEI_UrFFkq_1d6kC-4Xg7SLSB8ZZVDjTUqJC_ot8vaVfX4ZllJ2SHEYaPnXmbq8NZVotgoQ372eU8NCIa_7X7uGl8GS,PREF = ID = 6505d5000db18c8c:FF = 0:TM = 1358526181:LM = 1358526181:S = Nzb5yzBzXiKPLk48]}

太棒了

WebEngine 只是使用底层注册的 cookie 引擎!但是等等,真的是这样吗?在发出请求之前,让我们尝试添加一个 cookie...

THAT IS AWESOME

WebEngine simply uses the underlying registered cookie engine! But wait, is it really? Lets try adding a cookie, prior to making the request...

cookie_store.add(new URL(url).toURI(), new HttpCookie("testCookieKey", "testCookieValue"));

然后我看一下Wireshark中的请求...

Then I look at the request in Wireshark...

GET / HTTP/1.1
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/535.14 (KHTML, like Gecko) JavaFX/2.2 Safari/535.14
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Cache-Control: no-cache
Pragma: no-cache
Host: www.google.com
Connection: keep-alive

没有饼干给我:(

我做错了什么?

推荐答案

在 Oracle 的 Vasiliy Baranov 的帮助下,我设法解决了这个问题.瓦西里写信给我:

I have managed to solve this issue with the help of Vasiliy Baranov from Oracle. Vasiliy wrote to me:

尝试将 cookie 放入 java.net.CookieHandler.getDefault() 之后WebView 是第一次和调用之前实例化WebEngine.load,例如如下:

Try putting the cookie into java.net.CookieHandler.getDefault() after the WebView is instantiated for the first time and before the call to WebEngine.load, e.g. as follows:

WebView webView = new WebView();
URI uri = URI.create("http://mysite.com");
Map<String, List<String>> headers = new LinkedHashMap<String, List<String>>();
headers.put("Set-Cookie", Arrays.asList("name=value"));
java.net.CookieHandler.getDefault().put(uri, headers);
webView.getEngine().load("http://mysite.com");

这会将 cookie 永久放入存储中,它应该在每个后续请求中发送出去(假设服务器没有取消设置).

This will place the cookie into the store permanently, it should be sent out on every subsequent request (presumably provided that the server doesn't unset it).

Vasiliy 还解释说,WebView 将安装它自己的 CookieHandler 实现,同时保留放入默认的 cookie.

Vasiliy also explained that WebView will install it's own implementation of the CookieHandler, while retaining cookies put into the default one.

最后,他提到了一些非常有趣的事情:

Lastly, he mentions something quite intriguing:

不要浪费时间尝试使用 java.net.CookieManager,并且java.net.CookieStore.它们可能会导致许多问题网站,因为他们实施了错误的标准.

Do not waste your time trying to use java.net.CookieManager, and java.net.CookieStore. They are likely to cause problems with many sites because they implement the wrong standard.

此后我尝试使用谷歌搜索,但这似乎不是常识.如果有人能够提供更多详细信息,我将不胜感激.看起来很奇怪,因为CookieStoreCookieManager 似乎被很多软件使用.

I tried googling after this but it doesn't seem to be common knowledge. If anyone is able to provide more details I would be grateful. It seems weird, since it seems CookieStore and CookieManager are used by a lot of software out there.

这篇关于使用 JavaFX 的 WebEngine/WebView 设置 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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