简体   繁体   English

Java-我的Applet Cookie代码有什么问题?

[英]Java - What's wrong with my applet's cookies code?

Here's what I have: 这是我所拥有的:

// save the game as a browser cookie
public void savecookie(){
    try{
        CookieManager manager=new CookieManager();
        CookieHandler.setDefault(manager);
        CookieStore store=manager.getCookieStore();
        HttpCookie cookie=new HttpCookie("JujuSaveData",tocookie());
        cookie.setPath("/");
        cookie.setVersion(0);
        cookie.setSecure(false);
        cookie.setMaxAge(60*60*24*365*100);
        cookie.setComment("Juju save data");
        URL url=new URL("http://www.pineapplemachine.com");
        store.add(url.toURI(),cookie);
    }catch(Exception e){
        e.printStackTrace();
    }
}
// load the game from a cookie
public void loadcookie(){
    try{
        CookieManager manager=new CookieManager();
        manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
        CookieHandler.setDefault(manager);
        URL url=new URL("http://www.pineapplemachine.com");
        URLConnection connection=url.openConnection();
        connection.getContent();
        CookieStore store=manager.getCookieStore();
        List<HttpCookie> cookies=store.getCookies();
        for(HttpCookie cookie:cookies){
            if(cookie.getName().equals("JujuSaveData")){
                fromcookie(cookie.getValue());
                break;
            }
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

The applet itself is here: http://pineapplemachine.com/juju/juju.html 小程序本身在这里: http : //pineapplemachine.com/juju/juju.html

It's intended to save the game state as a cookie and load it later. 旨在将游戏状态另存为cookie并在以后加载。 That doesn't happen. 那不会发生。 I'm using chrome, and when I search for the cookie that should have been saved none comes up. 我使用的是chrome,当我搜索本应保存的Cookie时,却没有出现。

What have I got wrong? 我怎么了?

Your code looks ok for me, I think your problem is that your applet not accomplish security requirements. 您的代码对我来说还不错,我认为您的问题是您的applet无法满足安全要求。 According to oracle documentation : To access cookies, you must sign your applet JAR file and request permission to run outside of the security sandbox. 根据oracle文档 :要访问cookie,必须签署applet JAR文件并请求在安全沙箱外部运行的权限。

Hope this helps, 希望这可以帮助,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM