我下面有XML输入。我需要调用API并将其作为输入传递,但值会动态变化。如何构建此输入结构?
<?xml version="1.0" encoding="utf-8"?>
<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>
<Test xmlns="http://tempuri.org/">
<acc>test</acc>
<pass>abc</pass>
<xmlInvData>
<![CDATA[
<MyData>
<name>test</name>
<number>900</number>
</MyData>
]]>
</xmlInvData>
<username>test</username>
<password>123</password>
</Test>
</soap:Body>
</soap:Envelope>
我在C#中具有MyData类,这对于设置name
和number
值很有用。
但是如何形成完整的结构并传递给Api呼叫?肥皂:信封和肥皂盒?
HttpClient httpClient = new HttpClient();
string requestUri = "https://testurl";
var byteArray = Encoding.ASCII.GetBytes("username:password");
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
HttpResponseMessage response = await httpClient.PostAsync(requestUri, httpContent);
我需要了解如何在输入json上方形成httpContent。
假设您已建立数据结构并将其序列化为名为xml
的字符串:
var httpContent = new StringContent(xml, Encoding.UTF8, "application/xml");