Consume Web Service Using SOAP Request

Here is the simple sample of calling web service using SOAP protocol. I have created one Console Application in C# using visual studio 2015 to explain how to call the 3rd Party SOAP Web Service.

In this article, we are learning to call 3rd party Web Services only. So we will not create any Web Service here.

Problem: We should have below information about the 3rd party Web Service :

Web Service URL – https://abc.xyz.com/ws/6.3/ws_auth.php”;

Method name– login

Parameter Names – UserId, Password

Return Result – SessionId

Solution: To call this Web Service, we have to follow these steps:

Step 1: Create Web Request Object:

First we have to create a web request object to corresponding url send the request for web service over http. We define request object’s header, content type, Soap Action method as POST.

public HttpWebRequest CreateWebRequest()

{

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(Url);

webRequest.Headers.Add("SOAPAction", SOAPAction);

webRequest.ContentType = "text/xml;charset=\"utf-8\"";

webRequest.Accept = "text/xml";

webRequest.Method = "POST";

return webRequest;

}

 

Step 2: Create SOAP Request Body: 

To pass the input parameters, we make Soap Request Body. For this we create SAOP Envelope.

public class Parameter

{

public string Name { get; set; }

public string Value { get; set; }

}
public string CreateSoapEnvelope(List parameters)
        {
     string StrParameters = string.Empty;



      string MethodCall = "<" + Method + @" xmlns=""https://abc.xyz.com/3.6"">";


            string _soapEnvelope = @"
                                    ";

            foreach (Parameter parameter in parameters)
            {
                StrParameters = StrParameters + "<" + parameter.Name + ">" + parameter.Value + "";
            }

            MethodCall = MethodCall + StrParameters + "";
            StringBuilder sb = new StringBuilder(_soapEnvelope);
            sb.Insert(sb.ToString().IndexOf(""), MethodCall);
            return sb.ToString();
        }

Step 3: Invoke Web Server Method:

public string InvokeService(List parameters)
        {
            WebResponse response = null;
            string strResponse = "";
         
            HttpWebRequest req = CreateWebRequest();

            //write the soap envelope to request stream
            using (Stream stm = req.GetRequestStream())
            {
                using (StreamWriter stmw = new StreamWriter(stm))
                {
                    stmw.Write(CreateSoapEnvelope(parameters));
                }
            }
            //get the response from the web service
            response = req.GetResponse();
            Stream str = response.GetResponseStream();
            StreamReader sr = new StreamReader(str);
            strResponse = sr.ReadToEnd();

            XmlDocument xmlResult = new XmlDocument();
            xmlResult.LoadXml(strResponse);

            XmlNodeList elemList = xmlResult.GetElementsByTagName(@"nis:SessionId");

            string SessionId = elemList[0].InnerText;

            return SessionId;
        }

Step 4: Calling web server

     static void Main(string[] args)
        {
            var url = "https://abc.xyz.com/ws/6.3/ws_auth.php";
            var action = "https://abc.xyz.com/ws/6.3/ws_auth.php/login";
            var webMethod = "login";
            var parameters = new List();

            parameters.Add(new Parameter { Name = "UserId", Value = "xxxxxx" });
            parameters.Add(new Parameter { Name = "Password", Value = "xxxxxx" });

            LoginWebService loginWebservice = new LoginWebService(url, webMethod, action);
            var sessionId = loginWebservice.InvokeService(parameters);
            
            Console.WriteLine("Login is done successfully.\n\n");
            Console.WriteLine("Session Id = " + sessionId);
            Console.WriteLine("\n\n");

         }

Written by 

Leave a Reply

Your email address will not be published. Required fields are marked *