C#HttpClient处理瑞典字符åäö作出回应

时间:2017-05-15 13:22:28

标签: c#

在Burp中发送测试请求时,我在瑞典语中获得了åäö的正确值,但是使用HttpClient我只得到这个字符:

enter image description here

我尝试将Accept-Language标题设置为sv-SEsv,但结果相同。我也尝试过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;
        }
    }

}

更新

来自服务器的标题:

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

2 个答案:

答案 0 :(得分:2)

谢谢@RemusRusanu。工作代码:

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;
    }
}

答案 1 :(得分:1)

Ogglas的答案将解决åäö,但可能不适用于ÅÄÖ。 ISO-8859-1对应于任一
-Windows-1252西欧(Windows)。代码页1252

-iso-8859-1西欧(ISO)。代码页28591

为确保获得正确的编码,您可能需要使用Encoding.GetEncoding(int codePage)。有关不同编码的更多信息,请参见https://docs.microsoft.com/en-us/dotnet/api/system.text.encoding?view=netframework-4.8

以下是示例代码,其中响应字符串为ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!“#¤%&/()=?^ * _ :;> @£$ {[]} \〜| <,.-¨'++ < / p>

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);
        }
    }
}

这给出了输出:

ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+"
ABCDEFGHIJKLMNOPQRSTUVWXYZÿÿÿabcdefghijklmnopqrstuvwxyzåäö0123456789!"#¤%&/()=?^*_:;>@£${[]}\~|<,.-¨'´+

其中ÿ显示为黑色字符xC3?在我的日志中。