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

Posted 邓戈麟

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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");// 中间不能有空格

 

以上是关于An invalid character [32] was present in the Cookie value的主要内容,如果未能解决你的问题,请参考以下文章

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

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 C

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

Spring Boot报错:java.lang.IllegalArgumentException: An invalid character [..] was present in the Cooki

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