Cookie可在cookie header中使用,但不能从getCookies()中使用?

问题描述:

我在服务器端看到一些奇怪的行为,并想了解原因。Cookie可在cookie header中使用,但不能从getCookies()中使用?

在客户端:

document.cookie = 'test_cookie=' + '[AB]cd|ef-gh[IJ]' + '; path=/;'; 
document.cookie = 'test_cookie2=' + 'cd|ef-gh' + '; path=/;'; 

在服务器上:

headers = httpServletRequest.getHeaders() 
// iterate and print headers 

cookies = httpServletRequest.getCookies(); 
// iterate and print headers 

输出:

// Both are there on the header, so tomcat doesn't block it: 
... 
header: cookie: test_cookie=[AB]cd|ef-gh[IJ]; test_cookie2=cd|ef-gh 


// Only one shows up from getCookies() 
... 
cookie: test_cookie2=cd|ef-gh 
// no test_cookie ??? 

为什么我没有能够看到test_cookie2? 我可以在我将其设置在客户端之前进行uri编码,但我认为'['和']'允许cookie字符?

是否有更正确的方法来设置它?

这里的正确设置cookie的前端方式:

document.cookie = 'test_cookie="[AB]cd|ef-gh[IJ]"; path=/'; 

绕不包含特殊字符的cookie值双引号。