首页 > Windows开发 > 详细

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

时间:2017-01-06 18:27:08      阅读:596      评论:0      收藏:0      [点我收藏+]

问题

技术分享

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

PercentEncode 源码为:

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;
    }
}
 

查找问题

第三方工具

技术分享
 

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

 

C#中的URLEncode转义

C#中URLEncode,C#中有两种URLEncode,WebUlitityHttpUlitity

   [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.

 

解决

技术分享

使用以下代码URLEncode 来进行URLEncode

 

  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 有点过时,或者说自定义的,需要我们特殊处理。

 

附:阿里签名规则

技术分享

 

参考

Which characters make a URL invalid?

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

阿里云签名机制

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

原文:http://www.cnblogs.com/HQFZ/p/6256821.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!