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
- Go to "Offset", "All Programs", then click "Microsoft Visual Studio 2015".
- Click "File", "New", and then "Project...", and in the New Projection window, click console "C#" - ".
- 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:
- public HttpWebRequest CreateSOAPWebRequest()
- {
- HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost/Employee.asmx" );
- Req.Headers.Add together(@"SOAPAction:http://tempuri.org/Improver" );
- Req.ContentType ="text/xml;charset=\"utf-8\"" ;
- Req.Accept ="text/xml" ;
- Req.Method ="POST" ;
- return Req;
- }
Now permit'southward create the method to invoke the web service using SOAP request body
At present callthe above method from main method by writing the following lawmaking equally:
- static void Primary( cord [] args)
- {
- Program obj =new Program();
- Console.WriteLine("Please Enter Input values.." );
- int a = Convert.ToInt32(Console.ReadLine());
- int b = Catechumen.ToInt32(Console.ReadLine());
- obj.InvokeService(a, b);
- }
Now whole code in Programme.cs file will look like the following:
- using System;
- using System.IO;
- using System.Net;
- using Organization.Xml;
- namespace UsingSOAPRequest
- {
- public class Program
- {
- static void Main( string [] args)
- {
- Program obj =new Program();
- Console.WriteLine("Please Enter Input values.." );
- int a = Catechumen.ToInt32(Console.ReadLine());
- int b = Convert.ToInt32(Console.ReadLine());
- obj.InvokeService(a, b);
- }
- public void InvokeService( int a, int b)
- {
- HttpWebRequest request = CreateSOAPWebRequest();
- XmlDocument SOAPReqBody =new XmlDocument();
- SOAPReqBody.LoadXml(@"<?xml version=" "1.0" " encoding=" "utf-8" "?>
- <soap:Envelope xmlns:lather="" http:
- <lather:Body>
- <Addition xmlns="" http:
- <a>" + a + @" </a>
- <b>" + b + @" </b>
- </Addition>
- </soap:Body>
- </lather:Envelope>");
- using (Stream stream = request.GetRequestStream())
- {
- SOAPReqBody.Save(stream);
- }
- using (WebResponse Serviceres = request.GetResponse())
- {
- using (StreamReader rd = new StreamReader(Serviceres.GetResponseStream()))
- {
- var ServiceResult = rd.ReadToEnd();
- Console.WriteLine(ServiceResult);
- Console.ReadLine();
- }
- }
- }
- public HttpWebRequest CreateSOAPWebRequest()
- {
- HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(@"http://localhost/Employee.asmx" );
- Req.Headers.Add(@"SOAPAction:http://tempuri.org/Addition" );
- Req.ContentType ="text/xml;charset=\"utf-8\"" ;
- Req.Take ="text/xml" ;
- Req.Method ="Postal service" ;
- return Req;
- }
- }
- }
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