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

今天看公司项目的代码,对图片发送异步请求的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("+"," ");

 
  
//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
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值