I use HttpUtility.EncodeUrl to encode a querystring param. When I send request to the server then on the server side Uri.AbsoluteUri contains---in some cases---different querystring param values.
E.g. the to-be-escaped data string is
string data = "+-/*.,?!~"£$%^&*()_{}[]@'#<>\|`¬;:=";
HttpUtility.EncodeUrl(data) result is
%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
but when the request arrives on the server, HttpRequestMessage.RequestUri.AbsoluteUri contains different string:
%2b-%2f*.%2c%3f!~%22%c2%a3%24%25%5e%26*()_%7b%7d[]%40'%23%3c%3e%5c%7c%60%c2%ac%3b:%3d
I need to compare them for HMAC auth mechanism, so I fixed this with function
/// <summary>
/// Gets escaped request URL string.
/// </summary>
/// <param name="uri"></param>
/// <retus></retus>
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();
retu ret;
}
Can someone tell me, what causes the difference and if there is a better way to handle it, please, share your thoughts.
