【发布时间】:2020-06-22 06:51:15
【问题描述】:
我在控制台应用程序中有Vietnam char,它工作正常。
Console.OutputEncoding = Encoding.UTF8;
Console.WriteLine("BỐN TRĂM");
但是当我尝试在 api 调用中执行此操作时,它给出了错误的字符。
正确 = BỐN TRĂM 错误 = Bễ́N TRĂMapi调用代码-
HttpClient httpClient = new HttpClient();
var byteArray = Encoding.ASCII.GetBytes(apiUserName + ":" + apiPassword);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, APIURL);
request.Content = new StringContent(xmlRequestBody, Encoding.Unicode, "text/xml");
request.Content.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("charset", "utf-8"));
为什么它在 api 中工作错误?
【问题讨论】:
-
StringContent(xmlRequestBody, Encoding.UTF8,...?虽然我完全确定我误解了你的问题 -
看起来您将 UTF-16 放在字符集标题(最后一行)中。此外,您似乎有 2 个不同的 httpClient... 一个称为“httpClient”,另一个称为“ProcessInvoiceDetail.httpClient”,但您没有显示任何使用其中任何一个的代码。
-
让我更新问题
-
网络库不喜欢包含 UTF-16 的 Xml 的第一行。通常有效的是使用 ReadLine() 跳过 xml 的标识行,然后您可以使用标准 xml 类进行解析。