【问题标题】:How to pass xml data as content to HttpClient in c#如何在c#中将xml数据作为内容传递给HttpClient
【发布时间】:2020-04-24 05:39:00
【问题描述】:

我的 c# 代码调用 webservice -

        var content = new StringContent(req.Body.ToString(), Encoding.UTF8, "application/xml"); ;
        HttpClient httpClient = new HttpClient();
        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://mydemo.com/service.asmx?pk=listCustomer");

        var byteArray = Encoding.ASCII.GetBytes("username:password");

        httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

        request.Content = content;
        request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/xml");
        HttpResponseMessage response = await httpClient.SendAsync(request);

        // getting 500 error in response data at root level invalid

在邮递员中,我调用这个 azure 函数来传递 xml 输入。

xml 输入格式为 -

<?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>
    <listCustomer xmlns="http://tempuri.org/">
      <id>KH001</id>
      <fromDate>01/01/2018</fromDate>
      <toDate>01/01/2020</toDate>
    </listCustomer>
  </soap:Body>
</soap:Envelope>

【问题讨论】:

  • 您可以尝试使用它来读取 XML 正文吗? var content = await new StreamReader(req.Body).ReadToEndAsync();

标签: c# xml azure soap azure-function-app


【解决方案1】:

我用邮递员在本地测试功能,下面是我的代码。也许你可以试一试。

public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");



            string requestBody = await new StreamReader(req.Body).ReadToEndAsync();


            return new ContentResult { Content = requestBody, ContentType = "application/xml" };
        }

内容类型是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-14
    • 1970-01-01
    • 2020-02-11
    • 2014-04-14
    • 1970-01-01
    相关资源
    最近更新 更多