How can I solve this problem,this is my first question in stack overflow.I really hope someone can give me some suggestions to solve this problem.
-
1First please go through this https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors and https://stackoverflow.com/help/how-to-ask – Sarangan May 26 '20 at 10:23
-
Please do NOT post images, post your code – Nowhere Man May 26 '20 at 10:27
2 Answers
Regarding future cookies, your solution is simply to URL-encode or Base64-encode the value before setting it in the cookie, and then URL-decode or Base64-decode it at read.
So, when creating the cookie do:
Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));

- 868
- 1
- 8
- 24
Version 0 cookie values are restrictive in allowed characters. It only allows URL-safe characters. This covers among others the alphanumeric characters (a-z, A-Z and 0-9)
and only a few lexical characters, including -
, _
, .
, ~
and %
. All other characters are invalid in version 0 cookies.
Your best bet is to URL-encode those characters. This way every character which is not allowed in URLs will be percent-encoded in this form %xx
which is valid as cookie value.
So, when creating the cookie do:
Cookie cookie = new Cookie(name, URLEncoder.encode(value, "UTF-8"));
// ...
And when reading the cookie, do:
String value = URLDecoder.decode(cookie.getValue(), "UTF-8");
// ...
Refer to this link:
java.lang.IllegalArgumentException: Control character in cookie value or attribute

- 137
- 9