我是一名开发人员,当Cookie包含变音符字符(ä,ö,ü)时,我面临下面的例外,我尝试了许多解决方案和配置,但没有任何结果。
我使用Tomcat7
请给出任何解决方案
Feb 21, 2013 6:29:16 AM org.apache.coyote.http11.AbstractHttp11Processor process
SEVERE: Error processing request
java.lang.IllegalArgumentException: Control character in cookie value or attribute.
at org.apache.tomcat.util.http.CookieSupport.isHttpSeparator(CookieSupport.java:193)
at org.apache.tomcat.util.http.Cookies.getTokenEndPosition(Cookies.java:488)
at org.apache.tomcat.util.http.Cookies.processCookieHeader(Cookies.java:291)
at org.apache.tomcat.util.http.Cookies.processCookies(Cookies.java:168)
at org.apache.tomcat.util.http.Cookies.getCookieCount(Cookies.java:106)
at org.apache.catalina.connector.CoyoteAdapter.parseSessionCookiesId(CoyoteAdapter.java:919)
at org.apache.catalina.connector.CoyoteAdapter.postParseRequest(CoyoteAdapter.java:688)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:402)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1600)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
根据对另一个问题的评论,您需要升级到tomcat 8.0.15(或更高)并启用RFC 6455 cookie处理器。文档在这里:http://tomcat.apache.org/tomcat-8.0-doc/config/cookie-processor.html
在conf/context.xml中通过:
<Context>
<CookieProcessor className="org.apache.tomcat.util.http.Rfc6265CookieProcessor" />
</Context>
使用UTF-8编码
可以全局设置:
java -Dfile.encoding=UTF-8
或本地:
System.setProperty("file.encoding", "UTF-8");
byte inbytes[] = new byte[1024];
FileInputStream fis = new FileInputStream("the.location.of.your.cookie");
fis.read(inbytes);
同样,如果您手动编辑cookie,不要保存Ä。而是使用UTF-8等效的
System.out.println("u00c4");
Chars Unicode
------------------------------
Ä, ä u00c4, u00e4
Ö, ö u00d6, u00f6
Ü, ü u00dc, u00fc
ß u00df
现在我遇到了同样的问题,首先我尝试将tomcat 7升级到tomcat 8.5.51,它工作,但它提出了新的问题
java.lang.IllegalArgumentException: An invalid domain [.foo.bar.com] was specified for this cookie
因为我不需要cookie在我的情况下,所以我只是排除cookie在Nginx级别,它也可以解决这个问题
proxy_set_header Cookie "";