概述
Cookie中包含空格可能会出现空格之后的数据丢失的情况。
解决办法:使用URLEncoder.encode()
对cookie进行编码,再使用URLDecoder.decode()
进行解码
Response:
StringBuilder cookie = new StringBuilder();
for (BasicNameValuePair basicNameValuePair : basicNameValuePairs) {
cookie.append(URLEncoder.encode(basicNameValuePair.getName(), String.valueOf(Charsets.UTF_8)))
.append("=").append(URLEncoder.encode(basicNameValuePair.getValue(), String.valueOf(Charsets.UTF_8)))
.append(";");
}
response.setHeader("Set-Cookie", String.valueOf(cookie));
Request
URLDecoder.decode(cookie.getValue(), String.valueOf(Charsets.UTF_8))
问题就解决了。
有一个问题:
当服务器为Jetty的时候,通过`Response.addCookie(cookie)会报错:
Cookie cookie = new Cookie("lastVisitTime", DateFormatUtils.format(new Date(), Const.TIME_STAMP_FORMATTER));
cookie.setMaxAge(24 * 60 * 60);
response.addCookie(cookie);
HTTP ERROR 500
Problem accessing /spring-service/rs/Hello. Reason:
Server Error
Caused by:
java.lang.IllegalArgumentException: RFC6265 Cookie values may not contain character: [ ]
at org.eclipse.jetty.http.Syntax.requireValidRFC6265CookieValue(Syntax.java:136)
at org.eclipse.jetty.server.Response.addSetRFC6265Cookie(Response.java:269)
at org.eclipse.jetty.server.Response.addCookie(Response.java:231)
at filter.AppFilter.setCookie(AppFilter.java:83)
at filter.AppFilter.doFilter(AppFilter.java:68)
at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1629)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:533)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:548)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:190)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:1595)
at org.eclipse.jetty.server.handler.ScopedHandler.nextHandle(ScopedHandler.java:188)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1253)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:168)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:473)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:1564)
at org.eclipse.jetty.server.handler.ScopedHandler.nextScope(ScopedHandler.java:166)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1155)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:219)
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:126)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:530)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:347)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:256)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:102)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:247)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:140)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.run(EatWhatYouKill.java:131)
at org.eclipse.jetty.util.thread.ReservedThreadExecutor$ReservedThread.run(ReservedThreadExecutor.java:382)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:708)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:626)
at java.lang.Thread.run(Thread.java:748)
Powered by Jetty:// 9.4.8.v20171121
只能通过setHeader
的方式添加cookie
。
但是同样的代码,使用addCookie的方式放在tomcat服务器上是完全OK的。不知道啥问题。
最后
以上就是甜甜手套为你收集整理的Cookie中包含空格(特殊字符)的解决方案的全部内容,希望文章能够帮你解决Cookie中包含空格(特殊字符)的解决方案所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复