2017-10-11 43 views
0

我在服務器端看到一些奇怪的行爲,並想了解原因。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字符?

是否有更正確的方法來設置它?

回答

0

這裏的正確設置cookie的前端方式:

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

繞不包含特殊字符的cookie值雙引號。