如何解决使用.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"));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。