c#使用HttpRequestMessage避免不良编码



我正在从API调用获得响应,我正在使用HttpRequestMessage来设置我的"get"请求,然后HttpClient流响应并返回一个字符串。然而,在响应中,我得到u2019而不是',当我将此结果转换为excel(使用JsonConvert和CsvWriter)时,我得到的是€™而不是'在我的csv中。当请求API的响应时,我是否错过了标头级别的某些内容?

public static string GetResponse_CFRA(string oauth2_token, string apiKey, string uri)
{
var httpRequestMessage = new HttpRequestMessage
{
Method = HttpMethod.Get,
RequestUri = new Uri(uri),
Headers = {
{ "Authorization", $"Bearer {oauth2_token}"},
{ "x-api-key", apiKey}
}
};
// Get the response from the API
using (var client = new HttpClient())
{
try
{
var response = client.SendAsync(httpRequestMessage).Result;
HttpContent responseContent = response.Content;
var responsedata = responseContent.ReadAsStringAsync();
string data = responsedata.Result;
return data;
}
catch
{
string sorry = "Please call the admin";
return sorry;
}
}
}

u2019(Unicode字符U+2019)是右单引号,即'的稍微弯曲版本。

当编码为UTF-8时(这是。net默认使用的编码来写文件),它由字节序列0xE2 0x80 0x99表示。

然而,如果您将字节0xE2 0x80 0x99解释为不是UTF-8,而是使用Windows 1252代码页(这是Windows上默认的单字节代码页之一,取决于您的区域设置),0xE2映射到â, 0x80映射到, 0x99映射到

所以你的问题是你有一个使用UTF-8将字符编码为字节的文本文件,但Excel试图使用Windows-1252来读取它,它将不同的字符映射到这些字节。

告诉Excel将CSV文件解释为UTF-8,在文件开头添加UTF-8字节顺序标记(BOM,即序列0xEF 0xBB 0xBF),或者在保存文件时将编码从UTF-8更改为Windows-1252。

要使用带有BOM的UTF-8将字符串写入文件,您需要手动指定编码为Encoding.UTF8,例如:

File.WriteAllText(path, contents, Encoding.UTF8);

最新更新