Control character in cookie value or attribute
程序员文章站
2022-05-10 20:16:57
...
tomcat7中cookie写入中文引发Control character in cookie value or attribute异常
原因是有关中文编码的问题,中文采用的是unicode编码,而英文采用的是ASCII编码,所以当COOkie保存中文的时候需要对中文进行编码,
而且从Cookie中取出内容的时候也要进行解码,编码和解码可以使用
public static void CreateCookie(String username,HttpServletRequest req,HttpServletResponse resp,int sec) throws Exception{
String encode = URLEncoder.encode(username,"UTF-8");
Cookie usercookie=new Cookie("userKey", encode);
String encode1=URLEncoder.encode( md5Encrypt(username),"UTF-8");//对字体进行编码
Cookie ssidcookie=new Cookie("ssid", encode1);
usercookie.setMaxAge(sec);
ssidcookie.setMaxAge(sec);
resp.addCookie(usercookie);
resp.addCookie(ssidcookie)
}
if(cookie.getName().equals("userKey")){
account=URLDecoder.decode(cookie.getValue(),"utf-8");//对字体进行解码
}
if(cookie.getName().equals("ssid")){
ssid=URLDecoder.decode(cookie.getValue(),"utf-8");
}
上一篇: 逆序对的三种求法
下一篇: luooj P1655手套
推荐阅读
-
【已解决】:java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
-
An invalid XML character (Unicode: 0x10) was found in the value of attribute
-
Cookie:java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
-
Control character in cookie value or attribute