您的位置:首页 > 编程语言 > Java开发

关于java的URL编码的问题(URLencode,encode)

2014-01-10 13:49 302 查看
今天看公司项目的代码,对图片发送异步请求的URL的编码问题,看了看,又查了一些资料,希望能对URL编码有疑惑的人了解一下吧。AAAAAAAAAAAAAAAA:首先我是用java的URLencode.encode方法对其进行编码的。它的说明是Encodes a given string
s
in a x-www-form-urlencodedstring using the specified encoding scheme
enc
.All characters except letters ('a'..'z', 'A'..'Z') and numbers('0'..'9') and characters '.', '-', '*', '_' are converted intotheir hexadecimal value prepended by '%'. For example: '#'-> #. In addition, spaces are substituted by '+'BBBBBBBBBBBBBBB:但是呢URL传输需要字符串的规则是:1:Charactersin the unreserved character set as defined by(ALPHA, DIGIT, "-",".", "_", "~") MUST NOT be encoded.2:All other characters MUST be encoded.3:Thetwo hexadecimal characters used to represent encoded charactersMUST be upper case.不知道大家看出来没有有何区别,首先URLencode.encode是'.', '-', '*', '_'符号不编码,而URL传输需要字符串是 "-",".", "_", "~"不编码。那么可以这样的StringURL="www.baidu.com";String ENCODING="utf-8";
String strURL= URLEncoder.encode(URL, ENCODING).replace("*","*").replace("~", "~").replace("+"," ");
[/code]
//URLencode.encode 会将符号“~” 转变为“~”,所以我们要转回来,同时要将不转换的“*”
//转换一下。当然了,URLencode.encode 会将空格转换为“+”,但是“+”又不在BBBBBBBBBBBBBBB说明的
//"-", ".", "_", "~"里面,我们又必须将其转化。以上就是我的一点小小的看法,
//希望能帮到之前不懂的人。
//对了,我查询资料的网址告诉大家一下:
//href="http://tools.ietf.org/html/draft-hammer-oauth-10#section-3.6"
//为什么编码后需要再次处理
//http://oauth.googlecode.com/svn/code/java/core/commons/src/main/java/net/oauth/OAuth.java
//参考代码,进去后搜索RLEncoder.encode
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: