很多人在学习cookie时,经常发生这个错误,今天有人问我这个问题,所以我为大家把这个问题解决方法为 大家分享一下(网上没有具体的解决方案)。
错误代码:
Cookie cookie=new Cookie("name","value value2");
response.addCookie(cookie);
错误提示
java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
错误代码:
Cookie c=new Cookie("name","value,value2");
response.addCookie(c);
错误提示:
java.lang.IllegalArgumentException: An invalid character [44] was present in the Cookie value
……
由异常可以看出这个问题属于无效参数问题,在看后面的提示内容
An invalid character [44] was present in the Cookie value
cookie中存在无效的字符character [44],所以我们查询ASCII码表可知44表示“,”32表示空格
所以我们只需要将value中的对应字符替换掉即可,或进行编码