An invalid character [32] was present in the Cookie value

HTTP Status 500 – Internal Server Error



Type Exception Report

Message An invalid character [32] was present in the Cookie value

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value
	org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:182)
	org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:115)
	org.apache.catalina.connector.Response.generateCookieString(Response.java:1019)
	org.apache.catalina.connector.Response.addCookie(Response.java:967)
	org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386)
	com.itheima.lastaccesstime.LastAccessTimeServlet.doGet(LastAccessTimeServlet.java:43)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

Note The full stack trace of the root cause is available in the server logs.


Apache Tomcat/8.5.16

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获得当前时间
		Date date = new Date();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");// 中间不能有空格
		String currentTime = format.format(date);

		// 1.创建Cookie记录当前的最新访问时间
		Cookie cookie = new Cookie("lastAccessTime", currentTime);
		cookie.setMaxAge(60 * 10 * 500);
		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())) {
					lastAccessTime = cookie.getValue();
				}
			}
		}

		response.setContentType("text/html;charset=UTF-8");
		if (lastAccessTime == null) {
			response.getWriter().write("你是第一次访问");
		} else {
			response.getWriter().write("你上次的访问访问是:" + lastAccessTime);
		}
	}

解决办法:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-ddhh:mm:ss");// 中间不能有空格
时间: 2024-10-08 16:34:52

An invalid character [32] was present in the Cookie value的相关文章

cookie设置日期时间有空格报错:java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value

rt,代码及报错如下: java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 现暂时将"yyyy-MM-dd hh:mm:ss"中的空格用#替代,就能正常显示时间日期了...

java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value

今天在练习 cookie时意外的报了这个错. java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value1这句话的意思是 一个不识别的字符[32]出现在了cookie当中 由于tomcat的版本比较高,所以在addCookie时是不能使用空格的 而在ASCII码中32对应的就是空格.只要把后台代码中的空格删掉就可以了. 我的代码:是在cookie中添加了时间,而时间的格

【Cookie】java.lang.IllegalArgumentException An invalid character [32] was present in the Cookie value

创建时间:6.30 java.lang.IllegalArgumentException: An invalid character [32] was present in the Cookie value 报错原因: Tomcat 8.5版本,在cookie值中不能使用空格. 代码: 1 protected void doGet(HttpServletRequest request, HttpServletResponse response) 2 throws ServletException

java中Cookie使用问题(message:invalid character [32] was present in the Cookie value)

1. 问题描述 Servlet中执行下面一段代码: public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); System.out.println( new Date().toString()); Cookie c

异常:Invalid character found in the request target. The valid characters are defined in RFC 3986

一.背景 事情是这样的,前几天做一个基本的数据库“增删改查”的需求,前端传参的方式是“JSON字符串”,后端接收到此参数后,使用阿里巴巴fastjson进行解析,然后入库.需求很简单吧,但是偏偏遇到问题了. 我发现,JSON字符串里面无数组,纯粹的都是json结构的时候,即都是“{}”时,不会报错,传参入库没问题.但是只要传参的值里面有数组,即有“[]”的结构时,就报错.报错内容如下(我的tomcat版本是8.5.45): java.lang.IllegalArgumentException:

Tomcat 8 Invalid character found in the request target. The valid characters are defined in RFC 3986

终极解决方案: Invalid character found in the request target. The valid characters are defined in RFC 3986 25-Aug-2018 11:27:27.086 信息 [http-nio-8087-exec-5] org.apache.coyote.http11.Http11Processor.service Error parsing HTTP request header Note: further oc

ant导入Zookeeper到Eclipse错误path contains invalid character

首先在Zookeeper源码目录执行 ant eclipse 遇到错误 path contains invalid character 可以修改\zookeeper\build.xml 文件加入 <property name="ivy.default.ivy.user.dir" value="f:/antlvy"/> 另外可能出现不能switch enum的错误.尝试多切换几次jdk. build path->libraries->Add l

Invalid character found in method name. HTTP method names must be tokens

  o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header Note: further occurrences of HTTP header parsing errors will be logged at DEBUG level. java.lang.IllegalArgumentException: Invalid character found in method name. HTTP metho

The filename 未命名.ipa in the package contains an invalid character(s).

The filename 未命名.ipa in the package contains an invalid character(s).  The valid characters are: A-Z, a-z, 0-9, dash, period, underscore, but the name cannot start with a dash, period, or underscore 把ipa的名字换成中文就OK了,就这么简单.