使用.net核心代码调用api时,越南语char出错

问题描述

下面,我在.NET核心azure函数应用程序中有很好的代码

调用一个API并传递输入数据,但另一方面,当我检查从API插入的数据时,它的越南字符错误

string APIURL = "https://mydemo.com/insertcustomer.asmx";
string xmlHeader = "<soap:Envelope xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd = \"http://www.w3.org/2001/XMLSchema\" xmlns:soap = \"http://schemas.xmlsoap.org/soap/envelope/\"><soap:Body><ImportCustomer xmlns= \"http://tempuri.org/\">";
        string xmlRequestBody = xmlHeader + "<xmlCstData><![CDATA[<Invoices><AmountInWords>KHÔNG VIỆT NAM ĐỒNG</AmountInWords></Invoices>]]></xmlCstData></ImportCustomer></soap:Body></soap:Envelope>";


public static async Task<HttpResponseMessage> callAPI(string APIURL,string xmlRequestBody,ILogger log)
{
    try
    {

        var byteArray = Encoding.ASCII.GetBytes(apiUserName + ":" + apiPassword);
        Function1.httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",Convert.ToBase64String(byteArray));
        Function1.httpClient.DefaultRequestHeaders.Clear();

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post,APIURL);
        HttpContent httpContent = new StringContent(xmlRequestBody);

        httpContent.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
        httpContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("charset","utf-8"));

        HttpResponseMessage response = await Function1.httpClient.PostAsync(APIURL,httpContent);

        return response;
    }
    catch (Exception exception)
    {
        log.LogError("Error: " + exception.ToString());
        return new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }
}

AmountInWords = KHÔNG VIỆT NAM ĐỒNG的输入值,但是在插入门户网站之后,我可以看到AmountInWords = KHễNG VIỆT NAM ĐỒNG。唯一错误的字符是

解决方法

问题可能源于字符编码。您是否尝试过更改此行

httpContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("charset","utf-8"));

对此吗?

httpContent.Headers.ContentType.Parameters.Add(new NameValueHeaderValue("charset","unicode"));