问题描述
我无法检索cookie maxage它总是返回-1
I can't retrieve cookie maxage it always returns -1
创建cookie:
Cookie securityCookie = new Cookie("sec", "somevalue");
securityCookie.setMaxAge(EXPIRATION_TIME);
检索cookie:
Cookie[] cookies = request.getCookies();
if (cookies != null) {
for(int i=0; i<cookies.length; i++) {
Cookie cookie = cookies[i];
if ("sec".equals(cookie.getName())){
int age = cookie.getMaxAge();
}
}
}
我总是年龄= -1
当我检查firefox cookie到期时,我看到奇怪的日期。
also when i check in firefox cookie expiration i see strange date.
Thx
推荐答案
当浏览器将cookie发送回原始服务器时,它不包含任何年龄。因此,上面的检索代码没有收到最大年龄是合乎逻辑的:它不包含在请求中。
When a browser sends a cookie back to the origin server, it doesn't include any age. So it is logical that your "retrieve" code above does not receive a max age: it is not included in the request.
当从服务器收到cookie时,浏览器使用max age参数来确定cookie应保留多长时间;年龄永远不会传回服务器,过期的cookie就会被丢弃。处理请求时,如果要续订cookie的年龄,请在响应中重新包含cookie。
When the cookie is received from the server, the browser uses the max age parameter to determine how long the cookie should be kept; the age is never communicated back to the server, an expired cookie is simply discarded. When processing a request, if you want to renew the age of the cookie, reinclude the cookie in the response.
另请参阅向原始服务器发送Cookie部分在中。
Also see the section "Sending Cookies to the Origin Server" in the RFC.
这篇关于Cookie getMaxAge的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!