Как передать xml данные в виде содержимого в HttpClient в c# - PullRequest
0 голосов
/ 24 апреля 2020

мой c# код для вызова веб-службы -

        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>

1 Ответ

1 голос
/ 24 апреля 2020

Я проверяю функцию на местном с почтальоном, ниже мой код. Может быть, вы можете попробовать.

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" };
        }

И тип контента будет правильным.

enter image description here

enter image description here

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...