Using HttpClient with SOAP

拜拜、爱过 提交于 2021-02-11 15:21:28

问题


I have been trying to use the HTTPClient object in .Net Framework 4.7 to create a simple SOAP request. I have used the parameters in Postman and it works perfectly fine. Here's my code:

 string url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso";

        //SOAP envelope string
        string xmlSOAP =
           @"<? xml version = ""1.0"" encoding = ""utf-8"" ?>
           < soap : Envelope xmlns: soap = ""http://schemas.xmlsoap.org/soap/envelope/"" >
                < soap:Body >
                    < ListOfContinentsByName xmlns = ""http://www.oorsprong.org/websamples.countryinfo"">         
                    </ ListOfContinentsByName >
                </ soap:Body >
           </ soap:Envelope >";

        HttpClient httpClient = new HttpClient();
        HttpContent httpContent = new StringContent(xmlSOAP);
        HttpResponseMessage response;

        HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url);
        req.Headers.Add("SOAPAction", "");
        req.Method = HttpMethod.Post;
        req.Content = httpContent;
        req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("text/xml; charset=utf-8");

        // Here you will get the Reponse from service
        response = await httpClient.SendAsync(req);
        // Converting the response into text format
        var responseBodyAsText = await response.Content.ReadAsStringAsync();

Any idea why this works in postman but not using the HTTPClient? I referenced this from the microsoft example here (https://social.msdn.microsoft.com/Forums/windowsapps/en-US/15e0c692-c47c-443c-b96d-5c4f77e7ed66/how-to-sendreceive-soap-request-and-response-using-c-in-windows-phone-8?forum=wpdevelop).


回答1:


Your code looks fine and should work. Looks like the API server is down now.

Here's the same code with few fixes and improvements that just makes it stable

public class Program
{
    private static readonly HttpClient httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        string url = "http://webservices.oorsprong.org/websamples.countryinfo/CountryInfoService.wso";

        string xmlSOAP = @"<?xml version=""1.0"" encoding=""utf-8""?>
        <soap:Envelope xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/"">
            <soap:Body>
            <ListOfContinentsByName xmlns=""http://www.oorsprong.org/websamples.countryinfo""/>         
            </soap:Body>
        </soap:Envelope>";

        try
        {
            string result = await PostSOAPRequestAsync(url, xmlSOAP);
            Console.WriteLine(result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }

        Console.ReadKey();
    }

    private static async Task<string> PostSOAPRequestAsync(string url, string text)
    {
        using (HttpContent content = new StringContent(text, Encoding.UTF8, "text/xml"))
        using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url))
        {
            request.Headers.Add("SOAPAction", "");
            request.Content = content;
            using (HttpResponseMessage response = await httpClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead))
            {
                //response.EnsureSuccessStatusCode(); // throws an Exception if 404, 500, etc.
                return await response.Content.ReadAsStringAsync();
            }
        }
    }
}

Probably you may additionally fill SOAPAction e.g. with url, but it depends on server requirements.



来源:https://stackoverflow.com/questions/62987949/using-httpclient-with-soap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!