banner



How To Send/receive Soap Request And Response Using C#

BackgroundThere are many services bachelor today such equally WCF, Residual, Web API etc., only nonetheless Web Service plays an important role in cross platform application advice such using  SAP web service to provide data for other platform applications.

Previously, I had written many articles on web services, from creating to consuming web services, and information technology got a huge response. Now in this article we will acquire how to consume or call spider web services using SOAP asking with the help of HttpWebRequest course. In this approach sometimes we need to consume the third party spider web services where we don't know much about the endpoints and configuration of web services. In this article we will not create any web service because we have already created it. And then, if we want to learn the basics of the web services please refer to my previous articles.

Step one: Find the Web Service SOAP Asking Torso.

In this commodity, we are not going to create web service considering we have already created information technology and if you desire to create web service and larn almost it then please refer my preceding articles.

And then let's find out the Soap request torso which is used to pass input parameters and invoke the web service. I have 1 web service which is hosted in my local IIS. So paste your spider web service url into the browser and wait till the following response comes which is shown in the following paradigm.

In the above screenshot, you might have noticed that in that location are three spider web methods in the spider web service named Addition, EmployeeDetails and HelloWorld. Now click on one of the methods which you want to invoke. Subsequently clicking on web method information technology shows SOAP request and Soap Response trunk, find out the SOAP asking body which is shown in the following epitome.

In the to a higher place image, we are clearly observing that information technology shows all details how to make SOAP request and is also showing parameter values which are required to invoke service using HttpWebRequest. So, lets larn in cursory about those parameters .

  • HTTP Method: It's nothing just the what blazon of HTTP request you wants to make to the service such every bit Go , POST ,PUT or delete.
  • Host: It defines the url where the service is hosted , in our scenario our host is localhost , i.e http://localhost/Employee.asmx.
  • Content-Type: It defines what type of content type request you lot are making such as XML or json now in our scenario its text/xml.
  • Content-Length: Information technology defines the content length of request body.
  • SOAPAction: This is very important attribute to identify specific spider web method and call it from multiple spider web methods .
  • Lather Body: It contains the request and response torso.

I hope you learned virtually SOAP request parameter, Now copy the soap envelope part to use it as SOAP request which we volition employ in our console application.

Pace 2: Create the Panel application to call Web Service

  1. Go to "Offset", "All Programs", then click "Microsoft Visual Studio 2015".
  2. Click "File", "New", and then "Project...", and in the New Projection window, click console "C#" - ".
  3. Give the projection a name, such as "UsingSOAPRequest" or some other as you wish and specify the location.

Now open Programme.cs file and write the following method to create HttpWebRequest as:

  1. public  HttpWebRequest CreateSOAPWebRequest()
  2. {
  3.     HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost/Employee.asmx" );
  4.     Req.Headers.Add together(@"SOAPAction:http://tempuri.org/Improver" );
  5.     Req.ContentType ="text/xml;charset=\"utf-8\"" ;
  6.     Req.Accept ="text/xml" ;
  7.     Req.Method ="POST" ;
  8. return  Req;
  9. }

Now permit'southward create the method to invoke the web service using SOAP request body

  1. public void  InvokeService( int  a, int  b) {
  2.   HttpWebRequest asking = CreateSOAPWebRequest();
  3.   XmlDocument SOAPReqBody =new  XmlDocument();
  4.   SOAPReqBody.LoadXml(@"<?xml version="
  5. "1.0"
  6. " encoding="
  7. "utf-viii"
  8. "?>   < lather: Envelope xmlns: lather = " "
  9.    http:
  10.    < soap: Body >
  11.    < Improver xmlns =""
  12.    http:
  13.    < a >" + a + @"  < /a>   < b > " + b + @"  < /b>   < /Add-on>   < /soap:Trunk>   < /soap:Envelope>");
  14. using (Stream stream = asking.GetRequestStream()) {
  15.     SOAPReqBody.Relieve(stream);
  16.    }
  17. using (WebResponse Serviceres = asking.GetResponse()) {
  18. using (StreamReader rd = new  StreamReader(Serviceres.GetResponseStream())) {
  19.            var ServiceResult = rd.ReadToEnd();
  20.            Console.WriteLine(ServiceResult);
  21.            Console.ReadLine();
  22.        }
  23.    }
  24. }

At present callthe above method from main method by writing the following lawmaking equally:

  1. static void  Primary( cord [] args)
  2. {
  3.     Program obj =new  Program();
  4.     Console.WriteLine("Please Enter Input values.." );
  5. int  a = Convert.ToInt32(Console.ReadLine());
  6. int  b = Catechumen.ToInt32(Console.ReadLine());
  7.     obj.InvokeService(a, b);
  8. }

Now whole code in Programme.cs file will look like the following:

  1. using  System;
  2. using  System.IO;
  3. using  System.Net;
  4. using  Organization.Xml;
  5. namespace  UsingSOAPRequest
  6. {
  7. public class  Program
  8.     {
  9. static void  Main( string [] args)
  10.         {
  11.             Program obj =new  Program();
  12.             Console.WriteLine("Please Enter Input values.." );
  13. int  a = Catechumen.ToInt32(Console.ReadLine());
  14. int  b = Convert.ToInt32(Console.ReadLine());
  15.             obj.InvokeService(a, b);
  16.         }
  17. public void  InvokeService( int  a, int  b)
  18.         {
  19.             HttpWebRequest request = CreateSOAPWebRequest();
  20.             XmlDocument SOAPReqBody =new  XmlDocument();
  21.             SOAPReqBody.LoadXml(@"<?xml version=" "1.0" " encoding=" "utf-8" "?>
  22.             <soap:Envelope xmlns:lather="" http:
  23.              <lather:Body>
  24.                 <Addition xmlns="" http:
  25.                   <a>" + a + @" </a>
  26.                   <b>" + b + @" </b>
  27.                 </Addition>
  28.               </soap:Body>
  29.             </lather:Envelope>");
  30. using  (Stream stream = request.GetRequestStream())
  31.             {
  32.                 SOAPReqBody.Save(stream);
  33.             }
  34. using  (WebResponse Serviceres = request.GetResponse())
  35.             {
  36. using  (StreamReader rd = new  StreamReader(Serviceres.GetResponseStream()))
  37.                 {
  38.                     var ServiceResult = rd.ReadToEnd();
  39.                     Console.WriteLine(ServiceResult);
  40.                     Console.ReadLine();
  41.                 }
  42.             }
  43.         }
  44. public  HttpWebRequest CreateSOAPWebRequest()
  45.         {
  46.             HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost/Employee.asmx" );
  47.             Req.Headers.Add(@"SOAPAction:http://tempuri.org/Addition" );
  48.             Req.ContentType ="text/xml;charset=\"utf-8\"" ;
  49.             Req.Take ="text/xml" ;
  50.             Req.Method ="Postal service" ;
  51. return  Req;
  52.         }
  53.     }
  54. }

At present run the panel application,  then the console window will look like as follows and enter the input values as:

Now press enter button, it will give the response in XML format as follows:

In preceding console window you have seen spider web service response in the class of XML format which contains the output as 300. I hope from all above examples you have learned how to phone call spider web service using SOAP request in console application.

Notes

  • Download the Zip file of the sample application for a better understanding.
  • Utilise proper validation as per your demand.

Summary

I hope this article is useful for all readers, if you have whatever suggestions related to this commodity then please contact me.

Read more articles on ASP.NET Web Services

  • Consuming Web Service In Windows Awarding
  • Consuming Web Service In a Console Awarding
  • Consuming Web Service In WPF Application
  • Calling Spider web Service Using ScriptManager
  • Web Service Method Attribute Properties
  • Session Direction In Spider web Service

How To Send/receive Soap Request And Response Using C#,

Source: https://www.c-sharpcorner.com/article/calling-web-service-using-soap-request/

Posted by: elliottviaguld99.blogspot.com

0 Response to "How To Send/receive Soap Request And Response Using C#"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel