简体   繁体   中英

Connect to existing SOAP API service in .Net Core console application

I am creating a.Net Core console application where I need to connect to a SOAP service to call a POST endpoint and pass some info throug an XML envelope. The creator of the service gave me the WCF file that I imported in my project as a connected service but I am stuck and don't understand what I have to do next. (I have consumed and created WebApi services in the past using HttpClient to connect to them but never SOAP).

Using SOAP UI I add the WCF, update the URL and create a new POST request where I pass the XML and it connects without any problem. Bellow a screenshot of the SOAP UI page. 在此处输入图像描述

What can I reference to do the same thing in my console application?

May be it can help. So you need first call:

     //CREATE REQUEST ACTION
    private static HttpWebRequest CreateWebRequest(string url)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        //webRequest.Headers.Add("SOAPAction", action);
        webRequest.ContentType = "text/xml;charset=\"utf-8\"";
        webRequest.Accept = "text/xml";
        webRequest.Method = "POST";
        return webRequest;
    }



 //CREATE SOAP REQUEST BODY
    private static XmlDocument CreateSoapEnvelope(string xxxxx,string xxxxx)
    {
        XmlDocument soapEnvelopeDocument = new XmlDocument();
        
        string soap =
            @"<?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>
                <get_DeclarationInfo xmlns=""http://tempuri.org/"">
                 <input>
                    <EgbNo xmlns="""">"+xxxx+@"</EgbNo>
                    <VoenEgb xmlns = """" >"+xxx+@"</VoenEgb>
                    <AgencyCode xmlns="""">xxxxx</AgencyCode>
                    <Pasw xmlns="""">xxxxx</Pasw>
                  </input>
                </get_DeclarationInfo>
              </soap:Body>
            </soap:Envelope>";

        soapEnvelopeDocument.LoadXml(soap);
        return soapEnvelopeDocument;
    }

    //INSERT SOAP BODY TO WEB REQUEST
    private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
    {
        using (Stream stream = webRequest.GetRequestStream())
        {
            soapEnvelopeXml.Save(stream);
        }
    }
     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM