我使用HttpUtility.EncodeUrl来编码查询字符串参数。当我向服务器发送请求时,然后在服务器端Uri.AbsoluteUri包含---在某些情况下---不同的查询字符串参数值。
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 auth机制进行比较,所以我用函数
修复了它 /// <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;
}
有人可以告诉我,是什么导致了差异,如果有更好的方法来处理它,请分享你的想法。
答案 0 :(得分:0)
我认为将 Action 中的参数类型从Uri
更改为string
可以解决问题。
一个小实验:
public ActionResult Test()
{
string data = "+-/*.,?!~\"£$%^&*()_{}[]@'#<>\\|`¬;:=";
return RedirectToAction("GetEncodedParam", new { data = HttpUtility.UrlEncode(data) });
}
public ActionResult GetEncodedParam(string data)
{
var s = HttpUtility.UrlDecode(data); // here it's original string "+-/*.,?!~\"£$%^&*()_{}[]@'#<>\\|`¬;:="
return new ContentResult { Content = s };
}