关于Cookie的An invalid character [32] was present in the Cookie value异常
2017-07-23 15:34
387 查看
案例—显示用户上次登录时间
问题
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); }
结果如图:
第一次访问:
非第一次访问:
相关文章推荐
- java.lang.IllegalArgumentException: 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
- HTTP Status 500 - An invalid character [44] was present in the Cookie value
- Java Web学习--获取Cookie报错An invalid character [32] was present in the Cookie 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.
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the CDATA sectio
- org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the CDATA sectio
- XML解析出现An invalid XML character (Unicode: 0x**) was found in the comment.
- Lucene.Net 2.9.2.2 Invalid shift value in prefixCoded string (is encoded value really an INT?) 异常排查处理
- java.lang.IllegalStateException: The remote endpoint was in state [TEXT_FULL_WRITING] which is an invalid state for called method 解决办法
- Jsp 出现异常IllegalArgumentException:Control character in cookie value or attribute解决方法
- [Store:280041]An record was found in the file store with an invalid version of 5
- The filename 未命名.ipa in the package contains an invalid character(s).
- 关于JSP文件运行时抛出The value for the useBean class attribute XXX is invalid异常的分析
- tomcat7中cookie写入中文引发Control character in cookie value or attribute异常
- invalid character found in the request target 异常