How to make a Soap call on . Net Core

Asked

Viewed 141 times

1

I am trying to consume an external service that receives an xml Soap, but always returns error 500 (Internal server error). But if I test via SOAPUI works normally.

I have tested several types of call and none of it works. Would anyone know what it can be, or how to do it the right way?

in . net core, I created an api to receive the data and convert it into the xml object and call that external service. so I set up the api with xmlformatters

services.AddControllers()
    .AddXmlSerializerFormatters()
    .AddXmlDataContractSerializerFormatters();

my first attempt was this

httpClient.DefaultRequestHeaders
    .Accept
    .Add(new MediaTypeWithQualityHeaderValue("text/xml"));

var httpRequest = new HttpRequestMessage()
{
    RequestUri = new Uri(apiUrl),
    Method = HttpMethod.Post
};

httpRequest.Content = new StringContent(xml.ToString(), Encoding.UTF8, "text/xml");

httpRequest.Headers.Clear();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
httpRequest.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
HttpResponseMessage response = httpClient.SendAsync(httpRequest).Result;

my 2nd attempt was:

HttpResponseMessage response = await httpClient.PostAsync(apiUrl, new StringContent(xml, Encoding.UTF8, "text/xml"));

have tried other ways too. none works.

1 answer

1


Here’s an example that works on both . net framework and . net core.

string url = "";
string body = "";
string soapAction = "";
string soapXml = $@"<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>
                      {body}
                    </soap:Body>
                  </soap:Envelope>";


WebRequest wr = WebRequest.Create(url);    
byte[] buff = Encoding.UTF8.GetBytes(soapXml);    
wr.Timeout = 60000;
wr.Method = "POST";
wr.ContentType = "text/xml; charset=utf-8";
wr.ContentLength = buff.Length;
wr.Headers.Add("SOAPAction", soapAction);

using (var reqStream = wr.GetRequestStream())
{
    reqStream.Write(buff, 0, buff.Length);
}

using var resp = wr.GetResponse();
using var respStream = resp.GetResponseStream();
using var reader = new StreamReader(respStream, Encoding.UTF8);
var resposta = reader.ReadToEnd();
  • gave internal error 500 tb

  • 1

    -- Edit -- actually your code works. I did a test of an example flame from any site and it really worked. the problem must be in my upload xml. thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.