Parsing the exception of storing JSON string in cookie

Exception caused by a JSON string is stored in a cookie

Control character in cookie value or attribute exception, or An invalid character [34] was present in the cookie value exception, etc. So how to solve such a problem, let’s follow the code step by step to look at

in our store String value or a json String to the cookies first pass validateCookieValue this method, as shown below

private void validateCookieValue(String value) {

int start = 0;

int end = value.length();



if (end > 1 && value.charAt(0) == '"' && value.charAt(end - 1) == '"') {

start = 1;

end--;

}

char[] chars = value.toCharArray();

for (int i = start; i < end; i++) {

char c = chars[i];

if (c < 0x21 || c == 0x22 || c == 0x2c || c == 0x3b || c == 0x5c || c == 0x7f) {

throw new IllegalArgumentException(sm.getString(

"rfc6265CookieProcessor.invalidCharInValue", Integer.toString(c)));

}

}

}

we take a look at this way, when do array with value value to each of these characters is verified, which, according to the theory of the if statement is an exception is thrown directly, so there will be a we Control character in the cookie value or attribute. Among them, 0x21 and 0x22 are hexadecimal representation numbers, and the corresponding positions are 33 and 34 respectively;

First of all, do cookies contain ASCII encoding?Then we use GBK or UTF-8 encoding when we use parsing. When it comes to ASCII code 0X21 and 0X22, what is the corresponding character?We need to check the ASCII code comparison table. OK, let’s look at the corresponding ASCII table

In , the two marked in red are randomly specified. I believe you will know why you reported that exception after reading the picture you bought.

br>>

lencoder. Encode (name, “UTF-8 “)

Urlencoder. Encode (name,” UTF-8 “)

Urlencoder.

URLDecoder.decode(cookies[i].getName(),”utf-8″)


Read More: