[英]C# HttpClient handle swedish characters åäö in response
When sending a test request in Burp I get correct values for åäö in Swedish but using HttpClient I only get this character: 在 Burp 中发送测试请求时,我在瑞典语中获得了正确的 åäö 值,但使用 HttpClient 我只能得到这个字符:
I have tried setting the Accept-Language
header to sv-SE
and sv
but with the same result.我尝试将Accept-Language
标头设置为sv-SE
和sv
但结果相同。 I have also tried getting GetByteArrayAsync
and converting this to UTF-8 but no luck there neither.我也尝试过获取GetByteArrayAsync
并将其转换为 UTF-8,但也没有运气。
private HttpClient client = new HttpClient();
public HttpService()
{
client.DefaultRequestHeaders.Add("Accept-Language", "sv");
client.DefaultRequestHeaders.Add("Accept-Charset", "utf-8");
}
public string GetRequest(string url)
{
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
//Same result
//var byteArray = response.Content.ReadAsByteArrayAsync().Result;
//var result = Encoding.UTF8.GetString(byteArray, 0, byteArray.Length);
using (HttpContent content = response.Content)
{
var result = content.ReadAsStringAsync().Result;
return result;
}
}
}
Update:更新:
Headers from server:来自服务器的标头:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 31903
Content-Type: application/json
Expires: Sun, 14 May 2017 22:00:00 GMT
Server: Microsoft-IIS/8.5
Set-Cookie: ASPSESSIONIDCSACQTRA=<REMOVED>; path=/
X-Powered-By: ASP.NET
Date: Mon, 15 May 2017 13:10:18 GMT
Thank you @RemusRusanu.谢谢@RemusRusanu。 Working code:工作代码:
public string GetRequest(string url)
{
using (HttpResponseMessage response = client.GetAsync(url).Result)
{
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
var result = Encoding.GetEncoding("ISO-8859-1").GetString(byteArray, 0, byteArray.Length);
return result;
}
}
Ogglas answer will fix it for åäö but might not work for ÅÄÖ. Ogglas 的回答会为 åäö 修复它,但可能不适用于 ÅÄÖ。 ISO-8859-1 correspondes to either ISO-8859-1 对应于
- Windows-1252 Western European (Windows). - Windows-1252 西欧 (Windows)。 Code page 1252代码页 1252
or或者
- iso-8859-1 Western European (ISO). - iso-8859-1 西欧 (ISO)。 Code page 28591代码页 28591
To be sure to get the correct encoding, you might want to use Encoding.GetEncoding(int codePage)
instead.为了确保获得正确的编码,您可能需要改用Encoding.GetEncoding(int codePage)
。 For more information about different encoding see, https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.8有关不同编码的详细信息, 请参阅https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.8
Here is an example code where the response string is ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\\~|<,.-¨'´+这是一个示例代码,其中响应字符串是 ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\\~|<,.-¨'´+
using System.Net.Http;
using log4net;
using System.Text;
namespace Stackoverflow
{
public static class Enc {
private static readonly ILog log = LogManager.GetLogger(typeof(Enc));
public static string GetRequest(HttpClient client, string url, int codepage) {
using (HttpResponseMessage response = client.GetAsync(url).Result) {
var byteArray = response.Content.ReadAsByteArrayAsync().Result;
var result = Encoding.GetEncoding(codepage).GetString(byteArray, 0, byteArray.Length);
return result;
}
}
public static void Example(HttpClient client, string url) {
string result1 = GetRequest(client, url, 1252);
string result2 = GetRequest(client, url, 28591);
log.Debug(result1);
log.Debug(result2);
}
}
}
This gives output:这给出了输出:
ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+"
ABCDEFGHIJKLMNOPQRSTUVWXYZÿÿÿabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+
where ÿ shows up as a black character xC3?哪里 ÿ 显示为黑色字符 xC3? in my logs.在我的日志中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.