为什么cookie值部分在'@'之后被忽视?
当我读取 cookie 的值时,“@”后面的部分将被忽略。因此,如果我的 cookie 值是“abc@xyz”,当我检索值时,我只会得到“abc”
Cookie cookies [] = request.getCookies ();
pwd=cookies[0].getValue();
,而在 javascript 中,我可以轻松地将其读取为“abc@xyz”,甚至在浏览器中cookies,我可以看到cookie的值为“abc@xyz”。这里可能出了什么问题?
When I'm reading the value of a cookie, the part after '@' is being ignored. So, if my cookie value was "abc@xyz", I'm just getting "abc" when I'm retrieving value by
Cookie cookies [] = request.getCookies ();
pwd=cookies[0].getValue();
whereas, in javascript I'm able to easily read it as "abc@xyz" and even in browser cookies, I can see the value of cookie to be "abc@xyz". What could be wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的第一个猜测是与字符编码相关的问题。您是否尝试过对 cookie 值进行 urlencode 和 -decode ?
编辑:
您可以使用
URLDecoder.decode (cookies[0].getValue(), "utf-8")
检索cookie值。为了使其工作,当然必须首先对值进行编码:如果您要设置来自 Java 的 cookie 值,或通过
encodeURIComponent("abc@xyz")
设置来自 JavaScript 的值。我不知道 cookie 是如何设置的,因此您可能必须为您正在使用的任何平台弄清楚这一点。My first guess would be a problem related to character encoding. Have you tried to urlencode and -decode the cookie value?
EDIT:
You would retrieve the cookie value by using
URLDecoder.decode (cookies[0].getValue(), "utf-8")
.In order for that to work, the value must of course be encoded in the first place: Use
URLEncoder.encode("abc@xyz", "utf-8")
, if you're setting the cookie value from Java, orencodeURIComponent("abc@xyz")
to set the value from JavaScript. I don't know how the cookie is set, so you might have to figure this one out for whatever platform you're working on.