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

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

问题描述

我似乎找不到任何方式使用 WebEngine / WebView 。 API没有给出任何想法如何获得一个类似HttpRequest的对象来修改头(这是我在XML-RPC的应用程序中使用)或任何种类的cookie管理器。



此页面上没有问题似乎触及这个问题 - 有 ,但它只是在applet中禁用Cookie,以修复错误,我的应用程序在桌面btw。



我创建的唯一方法是通过请求第一页(需要一个sessionID的cookie才能正确加载) ),获取访问被拒绝的样式消息,在页面上下文中执行一些javascript设置cookie然后刷新。



如何使用WebEngine设置cookie?






更新:从上面链接的问题中,我尝试了解一些使用 CookieManager 和相关API。 我找到了此代码,然后我尝试将其合并到我的应用中,带有奇怪的结果;

  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:

  {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 ...

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

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

  GET / HTTP / 1.1 
Accept-Language:en-us,en; q = 0.5
Accept-Encoding:gzip
-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 )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

我没有cookie:(



我做错了什么?

解决方案

我已经成功地解决这个问题与Vasiliy Baranov从Oracle帮助Vasiliy写信给我:


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




  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永久存储到应用商店中,应在每次后续请求时发送



Vasiliy还解释说, WebView 会安装它自己的实现

最后,他提到了一些非常有趣的东西: p>


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


似乎不是常识。如果任何人能够提供更多的细节,我将不胜感激。这似乎很奇怪,因为看起来 CookieStore CookieManager 被很多软件使用。


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.

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.

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.

How do I set a cookie using WebEngine?


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();

Now lets say we do this:

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

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]}

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

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

No cookie for me :(

What am I doing wrong?

解决方案

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

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

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 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:

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.

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天全站免登陆