java.lang.IllegalArgumentException: Cookie

导读:本篇文章讲解 java.lang.IllegalArgumentException: Cookie,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

问题一

cookie

cookie 的设置:

Cookie cookie = new Cookie("my,cc","kelloMan");

错误日志

java.lang.IllegalArgumentException: Cookie name[my,sa]是一个保留令牌
	javax.servlet.http.CookieNameValidator.validate(Cookie.java:454)
	javax.servlet.http.Cookie.<init>(Cookie.java:168)
	com.github.tangmonkmeat.servlet.TestServlet.doGet(TestServlet.java:23)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

问题二

cookie

Cookie cookie = new Cookie("mycc","hello,Man");

错误日志

java.lang.IllegalArgumentException: Cookie值中存在无效字符[44]
	org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateCookieValue(Rfc6265CookieProcessor.java:197)
	org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:123)
	org.apache.catalina.connector.Response.generateCookieString(Response.java:974)
	org.apache.catalina.connector.Response.addCookie(Response.java:926)
	org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:385)
	com.github.tangmonkmeat.servlet.TestServlet.doGet(TestServlet.java:25)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

问题三

cookie

Cookie cookie = new Cookie("八戒","helloMan");

错误日志

java.lang.IllegalArgumentException: Cookie name[八戒]是一个保留令牌
	javax.servlet.http.CookieNameValidator.validate(Cookie.java:454)
	javax.servlet.http.Cookie.<init>(Cookie.java:168)
	com.github.tangmonkmeat.servlet.TestServlet.doGet(TestServlet.java:23)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
	org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)

原因


IllegalArgumentException – if the cookie name is null or empty or contains any illegal characters (for example, a comma, space, or semicolon) or matches a token reserved for use by the cookie protocol

大致意思:

IllegalArgumentException – 如果 Cookie 名称为空或空,或包含任何非法字符(例如,逗号、空格或分号)或匹配保留供 Cookie 协议使用的令牌

结论

cookie 的key 和 value都不可以包含任何非法字符(例如,逗号、空格或分号)或匹配保留供 Cookie 协议使用的令牌,还有 中文也会触发IllegalArgumentException

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/69743.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!