【问题标题】:How to pass XML input to API call using C#如何使用 C# 将 XML 输入传递给 API 调用
【发布时间】:2020-06-03 06:33:31
【问题描述】:

我有以下 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 类,可用于设置 namenumber 值。

但是我怎样才能形成一个完整的结构并传递给 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。

【问题讨论】:

    标签: c# .net c#-4.0 dotnet-httpclient


    【解决方案1】:

    假设您已经构建了数据结构并将其序列化为名为@9​​87654321@ 的字符串:

    var httpContent = new StringContent(xml, Encoding.UTF8, "application/xml");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-10
      相关资源
      最近更新 更多