阿里簽名中URLEncode於C#URLEncod不同之處


問題

QQ截圖20170106155741

如上圖所示,阿里雲的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;
    }
}
 

查找問題

第三方工具

QQ截圖20170106160805
 

上圖表明的確沒有轉義!(感嘆號)

 

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.

 

解決

QQ截圖20170106173506

使用以下代碼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 有點過時,或者說自定義的,需要我們特殊處理。

 

附:阿里簽名規則

image

 

參考

Which characters make a URL invalid?

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

阿里雲簽名機制


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM