阿里签名中URLEncode于C#URLEncod不同之处

问题

QQ截图20170106155741

如上图所示,阿里云的PercentEncode 转换! 为 %21

PercentEncode 源码为:

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
package com.aliyuncs.auth;
 
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
 
public class AcsURLEncoder {
     public final static String URL_ENCODING = "UTF-8" ;
     
     public static String encode(String value) throws UnsupportedEncodingException {
         return URLEncoder.encode(value, URL_ENCODING);
     }
     
     public static String percentEncode(String value) throws UnsupportedEncodingException{
         return value != null ? URLEncoder.encode(value, URL_ENCODING).replace( "+" , "%20" )
                 .replace( "*" , "%2A" ).replace( "%7E" , "~" ) : null ;
     }
}
01
  

查找问题

第三方工具

01
<a href= "http://images2015.cnblogs.com/blog/684558/201701/684558-20170106162851378-1970650580.png" ><img style= "background-image: none; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-width: 0px;" title= "QQ截图20170106160805" src= "http://images2015.cnblogs.com/blog/684558/201701/684558-20170106162852784-188184266.png" alt= "QQ截图20170106160805" width= "644" height= "170" border= "0" ></a>
01
  

上图表明的确没有转义!(感叹号)

 

C#中的URLEncode转义

C#中URLEncode,C#中有两种URLEncode,WebUlitity HttpUlitity

01
02
03
04
05
06
07
08
09
10
11
12
13
[TestFixture]
public class TestUlities
  {
      [Test]
      public void Test()
      {
          var url = @"http://img05.taobaocdn.com/bao/uploaded/TB2BVKlfFXXXXarXXXXXXXXXXXX_!!111708970-0-saturn_solar.jpg" ;
 
          var webUrlEncode = WebUtility.UrlEncode(url);
 
          var httpUrlEncode = HttpUtility.UrlEncode(url);
      }
  }

 

发现都没有转义!(感叹号)

 

WHY

 

In general URIs as defined by RFC 3986 (see Section 2: Characters) may contain any of the following characters:

ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~:/?#[]@!$&'()*+,;=`.

Any other character needs to be encoded with the percent-encoding (%hh). Each part of the URI has further restrictions about what characters need to be represented by an percent-encoded word.

 

解决

QQ截图20170106173506

使用以下代码URLEncode 来进行URLEncode

 

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
public class AliUrlEncodeHelper
   {
       public static string Encode( string str)
       {
           return ! string .IsNullOrEmpty(str) ?
               WebUtility.UrlEncode(str).Replace( "+" , "%20" )
               .Replace( "*" , "%2A" )
               .Replace( "%7E" , "~" )
               .Replace( "!" , "%21" )
               .Replace( "'" , "%27" )
               .Replace( "(" , "%28" )
               .Replace( ")" , "%29" )
               :str;
       }
   }

结论

阿里的URLEncode 有点过时,或者说自定义的,需要我们特殊处理。

 

附:阿里签名规则

image

 

参考

Which characters make a URL invalid?

Les codes hexas et unicode des caractères usuels, par Nicolas Hoffmann

阿里云签名机制



转载于          https://www.cnblogs.com/HQFZ/p/6256821.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值