C# 将HttpClient与Windows-1252编码的url一起使用
我正在尝试使用System.Net.HttpClient与一个非常简单的web API进行通信。这个API客户端有一个搜索功能,我可以通过它的名字搜索用户。问题是API在url中使用了Windows 1252编码,当我尝试在HttpClient中使用Windows 1252编码的url时,HttpClient在发出请求之前使用utf-8对url进行编码C# 将HttpClient与Windows-1252编码的url一起使用,c#,.net,xamarin,httpclient,C#,.net,Xamarin,Httpclient,我正在尝试使用System.Net.HttpClient与一个非常简单的web API进行通信。这个API客户端有一个搜索功能,我可以通过它的名字搜索用户。问题是API在url中使用了Windows 1252编码,当我尝试在HttpClient中使用Windows 1252编码的url时,HttpClient在发出请求之前使用utf-8对url进行编码 string name = "Arnar Aðalsteinsson"; // My name, note the 'ð' character
string name = "Arnar Aðalsteinsson"; // My name, note the 'ð' character
string encodedName = HttpUtility.UrlEncode(name, Encoding.GetEncoding(1252));
string url = string.Format("http://website.com/find?name={0}", encodedName);
// url = http://website.com/find?name=Arnar+A%f0dalsteinsson
string result = httpClient.GetStringAsync(url)
// result = "Name A%f0dalsteinsson is unknown", actual output from API service
// httpClient encodes the previously encoded parameter to A%25f0dalsteinsson
当我在浏览器中使用编码地址时,它工作正常
非常感谢您的帮助。我在尝试复制此内容时找不到问题,它在我这边工作正常,尝试使用查看您的请求结果,可能是服务器端的问题 这是我的密码:
string name = "Arnar Aðalsteinsson";
string encodedName = HttpUtility.UrlEncode(name, Encoding.GetEncoding(1252));
string url = string.Format("http://website.com/find?name={0}", encodedName);
HttpClient httpClient = new HttpClient();
try
{
**string result = httpClient.GetStringAsync(url).Result;**
}
catch (System.AggregateException ex) {
Debug.WriteLine(ex.Message);
}
结果:website.com/find?name=Arnar+A%f0alsteinson我创建了一个示例windows窗体应用程序,它可以按预期正常工作。字符串名称=Arnar Aðalsteinson;字符串encodedName=HttpUtility.UrlEncodename,Encoding.GetEncoding1252;stringurl=string.Format{1},session,encodedName;字符串结果=wait client.GetStringAsyncurl;这并不反映Xamarin Android中发生的情况。