url encode的问题
1.urlencode和decode
字符的编码和解码在有中文和特殊符号的情况下,常常是一个头疼的问题。url的encode和decode是解决这个问题的一个分支,通过简单的算法将特殊字符编码,其大致算法如下:
The alphanumeric characters “a” through “z”, “A” through “Z” and “0″ through “9″ remain the same.The special characters “.”, “-”, “*”, and “_” remain the same.The space character ” ” is converted into a plus sign “+”.All other characters are unsafe and are first converted into one or more bytes using some encoding scheme. Then each byte is represented by the 3-character string “%xy”, where xy is the two-digit hexadecimal representation of the byte. The recommended encoding scheme to use is UTF-8. However, for compatibility reasons, if an encoding is not specified, then the default encoding of the platform is used。简单来讲,就是将一个非英文的字符先用一定的编码方式(比如UTF-8)编码得到3个字节,然后每个字节的8位用两个16进制的字符来表示,前面再加上%。java处理伪代码描述如下:?
?
public void decodeQueryTo(MultiMap parameters){if (_query==_fragment)return;UrlEncoded.decodeTo(StringUtil.toString(_raw,_query+1,_fragment-_query-1,_encoding),parameters,_encoding);}
?
可以看到,会使用_encoding参数,这个就是前面new出EncdodeHttpUri的传入参数,即org.eclipse.jetty.util.URI.charset设置的参数。
因此,对于queryStr,如果请求中设置了_queryEncoding,就用他的编码,否则用系统参数org.eclipse.jetty.util.URI.charset设置的编码,否则用默认编码UTF-8
?
?
3.body部分
默认使用 UTF-8 编码,当然可以在使用之前使用 request.set CharacterEncoding 设定编码.
注:网上有资料说POST 参数默认使用 Content-type 中的 Charset 编码,但看了下源码,tomcat是有这个功能的,在getCharacterEncoding的时候,有一个如果为null则去ContentType中取的动作,但jetty好像没有)
?