Lucky Ncube
Lucky Ncube

Reputation: 118

SOAP Client using C#

I have Soap web service https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl. This has a method called process. My question is how do I create soap client to send a request, that submits/Retrieves information from the server. The example XML for the request is shown below:

<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
  <Request>
    <Transaction>
      <VersionNumber>1.0</VersionNumber>
      <Number>434252-342234-6765</Number>
      <SystemIdentifier>SYSTEM999</SystemIdentifier>
      <DestinationCode>APACE</DestinationCode>
      <ClientCountryCode>ZA</ClientCountryCode>
      <Timestamp TimeZone="Africa/Johannesburg">20160601123456</Timestamp>
      <TestIndicator>Y</TestIndicator>
      <User>ProviderX/Jane Doe</User>
    </Transaction>
    <MembershipLookup>
      <Funder>AFunder</Funder>
      <WithMembershipNumber>
        <MembershipNumber>123456789</MembershipNumber>
      </WithMembershipNumber>
    </MembershipLookup>
  </Request>
</Member>

The response message is structured as follows :

<?xml version="1.0" encoding="UTF-8"?>
<Member xmlns="urn:apace:member:format:1.0">
  <Response>
    <Transaction>
      <VersionNumber>1.0</VersionNumber>
      <Number>434252-342234-6765</Number>
      <Status>S</Status>
      <Timestamp TimeZone="Africa/Johannesburg">20160601123500</Timestamp>
    </Transaction>
    <Membership NumberOfBeneficiaryRecords="2">
      <Funder>AFunder</Funder>
      <MembershipNumber>123456789</MembershipNumber>
      <Beneficiary SequenceNumber="1">
        <DependentCode>00</DependentCode>
        <Type>P</Type>
        <Status>A</Status>
        <BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
      </Beneficiary>
      <Beneficiary SequenceNumber="2">
        <DependentCode>01</DependentCode>
        <Type>D</Type>
        <Status>A</Status>
        <BiometricEnrolmentStatus>Y</BiometricEnrolmentStatus>
      </Beneficiary>
    </Membership>
  </Response>
</Member>

Please help I am really stuck, Haven't used SOAP services with C#, I have tried examples online but the structure does not follow my message structures are more complicated

The actual message structure is structured as follows:

<soap:Envelope
    xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
    xmlns:apac="http://apace.systems/apacewebservices/"
    xmlns:urn="urn:apace:member:format:1.1">
    <soap:Header>
        <apac:secureToken>Token66657752</apac:secureToken>
    </soap:Header>
    <soap:Body>
        <apac:process>
            <apac:request>
                <Member
                    xmlns="urn:apace:member:format:1.1">
                    <Request>
                        <Transaction>
                            <VersionNumber>1.1</VersionNumber>
                            <Number>30074</Number>
                            <SystemIdentifier>LIFEHEALTH</SystemIdentifier>
                            <DestinationCode>HEALTH263</DestinationCode>
                            <ClientCountryCode>ZA</ClientCountryCode>
                            <Timestamp TimeZone="Africa/Johannesburg">20200705123456</Timestamp>
                            <TestIndicator>Y</TestIndicator>
                            <User>CIMSZW/Jane Doe</User>
                        </Transaction>
                        <MembershipLookup>
                            <IncludeDetail>Y</IncludeDetail>
                            <Funder>CIMSZWA</Funder>
                            <WithMembershipNumber>
                                <MembershipNumber>11117374</MembershipNumber>
                                <DependentCode>00</DependentCode>
                            </WithMembershipNumber>
                        </MembershipLookup>
                    </Request>
                </Member>
            </apac:request>
        </apac:process>
    </soap:Body>
</soap:Envelope>

Hw do I create the SOAP envelope and the Header part with the Security Key Token

Upvotes: 2

Views: 33025

Answers (2)

Junaid
Junaid

Reputation: 31

You have two options

  1. Add as service reference , it will genrate the
    add as a service reference
  1. use Visual studio developer prompt and consume the service using a proxy class

svcutil.exe https://test-submit.health263.systems:8081/apacewebservices/AMF1_0?wsdl /t:code /n:*,SampleNamespace /o:C:\Service\sampleServiceProxy.cs /config:C:\Service\sampleService.config /ct:System.Collections.Generic.List`1

Upvotes: 3

Gilvar
Gilvar

Reputation: 46

You can use a couple different system libraries to create an HttpWebRequest, create custom xml, and insert that xml into your request before you send it. (system.net, system.xml.linq, system.io)

I was able to hit your web service but got a 500 error. (Hopefully you see a log or that wasn't yours!)

Here's a simple class that can call a SOAP web service. It's almost the xml that you need but might need some tweaking. If you're having issues making custom xml this may be a possible solution.

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Xml.Linq;

namespace ConsoleApp1
{
    /// <summary>
    /// Random class
    /// </summary>
    class Class1
    {
        /// <summary>
        /// Function that calls a SOAP web service
        /// </summary>
        public void CallSOAP()
        {
            try
            {
                // Construct http post request
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(new Uri("https://test-submit.health263.systems:8081/apacewebservices/AMF1_0"));
                request.Method = "POST";
                request.ContentType = "application/xml";
                request.Accept = "application/xml";

                // Setting a namespace for your xml
                // I'm not sure the timezone one is set up properly
                XNamespace soap = "urn:apace:member:format:1.0";
                XNamespace timezone = "TimeZone=\"Africa/Johannesburg\"";

                // This constructs your xml using the LINQ library. I modeled after your demo, but needs tweaking as I get 500 error from server.
                XElement requestXML =
                    new XElement(soap + "Member",
                        new XElement("Request",
                            new XElement("Transaction",
                                new XElement("VersionNumber", "1.0"),
                                new XElement("Number", "434252 - 342234 - 6765"),
                                new XElement("SystemIdentifier", "SYSTEM999"),
                                new XElement("DestinationCode", "APACE"),
                                new XElement("ClientCountryCode", "ZA"),
                                new XElement(timezone + "Timestamp", "20160601123456"),
                                new XElement("TestIndicator", "Y"),
                                new XElement("User", "ProviderX/Jane Doe")
                            ),
                            new XElement("MembershipLookup",
                                new XElement("Funder", "AFunder"),
                                new XElement("WithMembershipNumber",
                                    new XElement("MembershipNumber", 123456789)
                                )
                            )
                        )
                    );

                // Convert the xml into a stream that we write to our request
                byte[] bytes = Encoding.UTF8.GetBytes(requestXML.ToString());
                request.ContentLength = bytes.Length;
                using (Stream putStream = request.GetRequestStream())
                {
                    putStream.Write(bytes, 0, bytes.Length);
                }

                // Execute the request and get an xml response "reader". You can read all xml at once or line by line
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {

                    var streamData = reader.ReadToEnd();

                }
            }
            catch (Exception ex)
            {
                // Write exception to console & wait for key press
                Console.WriteLine(ex.Message + ex.StackTrace);
                Console.ReadKey();
            }
        }
    }
}

Upvotes: 2

Related Questions