关于Cookie的An invalid character [32] was present in the Cookie value异常
来源:互联网 发布:api原油库存数据预测 编辑:程序博客网 时间:2024/05/18 12:30
案例—显示用户上次登录时间
问题
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String nowtime=sdf.format(new Date()); Cookie cookie=new Cookie("lasttime", nowtime); cookie.setMaxAge(3600*24*7); response.addCookie(cookie);
这段代码的报的异常java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value。 经检查发现是response.addCookie(cookie);的cookie的值不能出现空格,就是说将SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");里格式的空格去掉就没问题。 也可以在获取当前登录时间时,使用URLEncoder编码,再设置为cookie的值;在获取cookie的value值的时候,使用URLDecoder解码。核心代码如下:
核心代码
使用javaweb实现:
//doGet内代码 //获得当前时间 Date date = new Date(); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String currentTime = format.format(date); // 使用URLEncoder编码,防止cookie的value出现 // An invalid character [32] was present in the Cookie value异常 currentTime = URLEncoder.encode(currentTime, "UTF-8"); //1、创建Cookie 记录当前的最新的访问时间 Cookie cookie = new Cookie("lastAccessTime", currentTime); cookie.setMaxAge(3600*24*7); response.addCookie(cookie); //2、获得客户端携带cookie ---- lastAccessTime String lastAccessTime = null; Cookie[] cookies = request.getCookies(); if(cookies!=null){ for(Cookie coo : cookies){ if("lastAccessTime".equals(coo.getName())){ // 使用URLDecoder解码 lastAccessTime = URLDecoder.decode(coo.getValue(), "UTF-8"); } } } response.setContentType("text/html;charset=UTF-8"); if(lastAccessTime==null){ response.getWriter().write("您是第一次访问"); }else{ response.getWriter().write("您上次的访问的时间是:"+lastAccessTime); }
结果如图:
第一次访问:
非第一次访问:
阅读全文
0 0
- 关于Cookie的An invalid character [32] was present in the Cookie value异常
- Java Web学习--获取Cookie报错An invalid character [32] was present in the Cookie value
- java.lang.IllegalArgumentException: An invalid character [44] was present in the Cookie value
- An invalid character [34] was present in the Cookie value] with root cause
- tomcat7中cookie写入中文引发Control character in cookie value or attribute异常
- tomcat7中cookie写入中文引发Control character in cookie value or attribute异常
- IllegalArgumentException: Control character in cookie value or attribute Tomcat7中文写入cookie时引发异常
- Control character in cookie value, consider BASE64 encoding your value 异常
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- All About JAVA An invalid XML character (Unicode: 0xdd65) was found in the comment 错误
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- Jsp 出现异常IllegalArgumentException:Control character in cookie value or attribute解决方法
- Control character in cookie value, consider BASE64 encoding your value
- Control character in cookie value, consider BASE64 encoding your value
- Control character in cookie value, consider BASE64 encoding your value
- jvm系列(一):java类的加载机制
- 几何模板
- poj1113 Wall 凸包
- NYOJ 6-喷水装置(一)(简单贪心)
- JAVA企业面试题精选 Java SE 51-60
- 关于Cookie的An invalid character [32] was present in the Cookie value异常
- 2017ACM福建省赛FZU2283 Tic-Tac-Toe
- 程序设计与算法(一)第一周测验(2017夏季) 解题报告
- 常见数字IC设计,FPGA工程师面试考题
- STM32F103mini教程学习总结与心得(一)
- hihocoder 1109 : 最小生成树三·堆优化的Prim算法
- Python 介绍及基本语法
- 关于梯度下降优化算法momentum的一些疑
- 线段树基本入门知识