0
我使用HttpUtility.EncodeUrl來編碼查詢字符串參數。當我向服務器發送請求時,然後在服務器端Uri.AbsoluteUri包含---在某些情況下---不同查詢字符串參數值。Uri.AbsoluteUri與請求的URL不同
E.g.待逸出數據串是
string data = "+-/*.,?!~\"£$%^&*()_{}[]@'#<>\\|`¬;:=";
HttpUtility.EncodeUrl(數據)的結果是
%2b-%2f*.%2c%3f!%7e%22%c2%a3%24%25%5e%26*()_%7b%7d%5b%5d%40%27%23%3c%3e%5c%7c%60%c2%ac%3b%3a%3d
但是當請求到達服務器上,HttpRequestMessage.RequestUri.AbsoluteUri包含不同的字符串:
%2b-%2f*.%2c%3f!~%22%c2%a3%24%25%5e%26*()_%7b%7d[]%40'%23%3c%3e%5c%7c%60%c2%ac%3b:%3d
我需要比較他們對HMAC身份驗證機制,使我修正了這個與功能
/// <summary>
/// Gets escaped request URL string.
/// </summary>
/// <param name="uri"></param>
/// <returns></returns>
private string GetEscapedRequestUrl(Uri uri)
{
/**
* When client uses HttpUtility.UrlEncode() some chars in Uri.AbsoluteUri on server side are NOT url-endcoded.
* E.g. +-/*.,?!~"£$%^&*()_{}[]@'#<>\|`¬;:=
* client-side: %2b-%2f*.%2c%3f!%7e%22%c2%a3%24%25%5e%26*()_%7b%7d%5b%5d%40%27%23%3c%3e%5c%7c%60%c2%ac%3b%3a%3d
* server-side: %2b-%2f*.%2c%3f!~ %22%C2%A3%24%25%5E%26*()_%7B%7D[ ] %40' %23%3C%3E%5c%7C%60%C2%AC%3b: %3d //note: spaces are added to show the difference
*/
string ret = string.Empty;
StringBuilder sb = new StringBuilder();
sb.Append(uri.Scheme);
sb.Append("://");
sb.Append(uri.Authority);
sb.Append(uri.LocalPath);
sb.Append(uri.Query
.Replace("~", "%7e")
.Replace("[", "%5b")
.Replace("]", "%5d")
.Replace("'", "%27")
.Replace(":", "%3a")
);
ret = sb.ToString();
return ret;
}
有人可以告訴我,是什麼引起了差異,如果有更好的方法來處理它,請分享您的想法。