为什么我的Cookie始终为空?

时间:2016-02-28 17:06:26

标签: gwt

我不明白。一小时前它工作了,突然之间我无法取回我刚才设置的cookie。在Chrome下我可以看到Cookie实际上存在,但如果我尝试将其恢复为null

private void setLoggedInCookie(String sessionId) {
    String domain = this.getDomain();

    Cookies.setCookie(ApiParameters.LOGIN_COOKIE, sessionId, expires, domain, "/", true);
    String cookie = Cookies.getCookie(ApiParameters.LOGIN_COOKIE);

    // Getting NOTHING from this ..
    for (String string : Cookies.getCookieNames()) {
        LOGGER.info("Cookie name: " + string);
    }

    if(cookie == null) {
        throw new RuntimeException("Cookie is 'null'.");
    }
}

private String getDomain() {
    LOGGER.fine("Host name: " + Window.Location.getHostName());
    String domain = Window.Location.getHostName().replaceAll(".*//", "").replaceAll("/", "").replaceAll(":.*", "");
    return "localhost".equalsIgnoreCase(domain) ? "localhost" : domain;
}

发生了什么事?

1 个答案:

答案 0 :(得分:2)

您传递域名" null"。浏览器仅允许访问与当前页面域相关联的cookie。由于您尝试从不是" null"的页面访问它,因此您无法获得它。

此外,请确保您尝试使用SSL访问它,因为您设置了" secure"参数为true。