【问题标题】:Java - What's wrong with my applet's cookies code?Java - 我的小程序的 cookie 代码有什么问题?
【发布时间】:2014-04-08 16:46:24
【问题描述】:

这是我所拥有的:

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

小程序本身在这里:http://pineapplemachine.com/juju/juju.html

它旨在将游戏状态保存为 cookie 并稍后加载。这不会发生。我正在使用 chrome,当我搜索应该保存的 cookie 时,没有出现。

我做错了什么?

【问题讨论】:

    标签: java cookies applet


    【解决方案1】:

    您的代码对我来说看起来不错,我认为您的问题是您的小程序没有满足安全要求。根据oracle documentation:要访问cookies,您必须签署您的applet JAR文件并请求在安全沙箱之外运行的权限。

    希望这会有所帮助,

    【讨论】:

      猜你喜欢
      • 2015-06-04
      • 2019-06-06
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 2021-07-04
      相关资源
      最近更新 更多