response中添加cookie时IllegalArgumentException错误

来源:互联网 发布:cloudify 教程 ubuntu 编辑:程序博客网 时间:2024/05/21 22:46

很多人在学习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中的对应字符替换掉即可,或进行编码

1 0
原创粉丝点击