使用 XML 从 VS2019 中的控制台应用程序调用简单的 POST API

Call simple POST API from console App in VS2019 with XML

我提到了Trying to call simple POST API from console App in VS2019。但是,需要在 post 而不是 JSON 中传递 XML 方法。有什么建议吗?

 static async Task Main(string[] args)
    {
        var TicketTask = await createTicket();
    }

    async static Task<string> createTicket()
    {
        var content = "unknown error";
        using (var httpClient = new System.Net.Http.HttpClient())
        {
            using (var request = new System.Net.Http.HttpRequestMessage(new HttpMethod("POST"), "http://1.0.01.1/slive/"))
            {
                try
                {
                    var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
                    request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

                  
                    request.Content =  new StringContent("<soapenv:Envelope xmlns:xsi...", Encoding.UTF8, "application/xml"); ????? need to post a xml method here 
                    request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
                    ServicePointManager.Expect100Continue = true;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                    HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
                    content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    Console.WriteLine(response);
                }
                catch (Exception ex)
                {
                    content = ex.Message;
                }
            }
        }
        return content;

找到解决方法,如果有更好的方法请指正:

async static Task<string> createTicket2()
{
    var content = "unknown error";
    using (var httpClient = new System.Net.Http.HttpClient())
    {
        using (var request = new System.Net.Http.HttpRequestMessage(new HttpMethod("POST"), "http://10/sap-lve/"))
        {
            try
            {
                var base64authorization = Convert.ToBase64String(Encoding.ASCII.GetBytes("an:s"));
                request.Headers.TryAddWithoutValidation("Authorization", $"Basic {base64authorization}");

                String str1 = @"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><Get_api_version xmlns='http://e.s.a.com'/></s:Body></s:Envelope>";

                **request.Content = new StringContent(str1, Encoding.UTF8, "text/xml");** ;
                request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml");
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                HttpResponseMessage response = await httpClient.SendAsync(request).ConfigureAwait(false);
                content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                //Console.WriteLine(response);
                var result = response.Content.ReadAsStringAsync();
                Console.WriteLine(result.Result);

            }
            catch (Exception ex)
            {
                content = ex.Message;
            }
        }
    }
    return content;
}