简体   繁体   English

Cookie可以在Cookie标头中使用,但不能从getCookies()中获得?

[英]Cookie available in cookie header but not from getCookies()?

I'm seeing some odd behavior with a cookie on the server side, and would like to understand why. 我在服务器端看到了Cookie的一些奇怪行为,并且想了解原因。

On the client: 在客户端上:

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

On the server: 在服务器上:

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

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

Output: 输出:

// 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 ???

Why am I not able to see the test_cookie2? 为什么我看不到test_cookie2? I could uri-encode before I set it on the client, but I thought '[' and ']' were allowed cookie characters? 在客户端上进行设置之前,我可以进行uri编码,但是我认为'['和']'被允许使用cookie字符吗?

Is there a more correct way to set it? 有没有更正确的设置方法?

Here's the way to set the cookie correctly on the frontend: 这是在前端正确设置cookie的方法:

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

Not the double quotes around the cookie value that contains the special characters. 不是在包含特殊字符的cookie值周围用双引号引起来。

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

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