Caused by: java.lang.IllegalArgumentException: cookie 值或属性中的控制字符

Caused by: java.lang.IllegalArgumentException: Control character in cookie value or attribute

enter image description here

enter image description here

我该如何解决这个问题,这是我在堆栈中的第一个问题overflow.I真的希望有人能给我一些解决这个问题的建议。

关于未来的 cookie,您的解决方案是在将值设置到 cookie 之前对值进行 URL 编码或 Base64 编码,然后在读取时对其进行 URL 解码或 Base64 解码.

因此,在创建 cookie 时执行以下操作:

Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));

版本 0 cookie 值对允许的字符有限制。它只允许 URL 安全字符。其中包括字母数字字符 (a-z, A-Z and 0-9) 和少数词汇字符,包括 -_.~%。所有其他字符在版本 0 cookie 中均无效。

最好的办法是 URL 对这些字符进行编码。这样,URLs 中不允许的每个字符都将以这种形式进行百分比编码 %xx,作为 cookie 值是有效的。

因此,在创建 cookie 时执行:

Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));
// ...

并且在读取 cookie 时,执行:

String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
// ...

参考这个link:

java.lang.IllegalArgumentException: Control character in cookie value or attribute