使用HttpClient.SendAsync时,request实参中的Uri斜杠('/')被Url解码导致请求Url错误,返回404,如何防止这种解码?
4.5已经试过没有这个问题.
以下是代码片段:
apiClient = new HttpClient {BaseAddress = new Uri("http://...")};
var request = new HttpRequestMessage(HttpMethod.Post, "accessToken/getAccessToken");
request.AddMatrixParam(new Dictionary<string, string>
{
["encryptStr"] = encryptStr,
["timestamp"] = timestamp.ToString()
});
AddMatrixParam方法是将字典转化为Matrix格式的url参数,然后拼接到url上,这个方法的操作结果确认没有问题
Debug.WriteLine($"Request URL: {request.RequestUri}");
var response = apiClient.SendAsync(request).Result;
Debug.WriteLine($"Request URL: {request.RequestUri}");
输出:
Request URL: accessToken/getAccessToken;encryptStr%3dPoB3dUXFGAaN3ZEcovdYgBp6GSu6ECvqIaSPvLgEWp5Ml%2fAkD5sbqWlqUTcqUoV0CTU3%2b%2bUvVYA%2f94bjj%2fAHv%2bXnFRIT8xjFCiKgLG94sx8%3d%3btimestamp%3d1565949032
Request URL: http://.../accessToken/getAccessToken;encryptStr=PoB3dUXFGAaN3ZEcovdYgBp6GSu6ECvqIaSPvLgEWp5Ml/AkD5sbqWlqUTcqUoV0CTU3++UvVYA/94bjj/AHv+XnFRIT8xjFCiKgLG94sx8=;timestamp=1565949032
请求发送前后的相对url部分是不一样的,"/"被莫名其妙得解码了出来