如何在 C# 中将 xml 数据作为内容传递给 HttpClient
How to pass xml data as content to HttpClient in c#
我调用 web 服务的 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>
我用postman在本地测试功能,下面是我的代码。也许你可以试试。
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" };
}
而且内容类型应该是正确的。
我调用 web 服务的 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>
我用postman在本地测试功能,下面是我的代码。也许你可以试试。
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" };
}
而且内容类型应该是正确的。