Click here to Skip to main content
15,884,237 members
Articles / Web Development / ASP.NET

Consume WebService Using SOAP

Rate me:
Please Sign up or sign in to vote.
2.62/5 (38 votes)
27 Feb 2005CPOL 216.6K   27   16
Consume WebService Using SOAP

Introduction

Web Services are thought of as a means to provide easily accessible services over a network. We can use VS.NET IDE to create a Web Service.

While there are different techniques to communicate with a Web Services, SOAP is regarded as the actual standard. SOAP messages are being sent to service endpoints. This can simply be SOAP over HTTP.

Information Regarding Exsiting Web Service

.Asmx file for that WebService (URL):

http://localhost/AddNumbers/Service1.asmx

WSDL URL:

http://localhost/AddNumbers/Service1.asmx ?wsdl

Namespace of web service:

http://localhost/wwwroot/addnumbers/Service1

Method:

C#
int AddTwoNumbers (int, int)

Class:

C#
Service1

Steps For Consuming Web Service Using SOAP

  1. Start a new ASP.NET web application.
  2. Install msxml.msi, then click on “Add reference “ and then select “Interop.MSXML2.dll” and click on add.

    You can download the English version of the msxml.msi install file via this link: http://download.microsoft.com/download/7/a/7/7a72ca2e-39cc-4600-ab36-bf3fea876a65/msxml3.msi.

  3. Create one label to display result (ID = lablel1).
  4. Declare objXMLHttp of type MSXML2.ServerXMLHTTP40.
    C#
    Protected MSXML2.ServerXMLHTTP40 objXMLHttp
    
  5. Declare one string strSoapEnvelope which contains soap request.
    C#
    string strSoapEnvelope= "";
    
  6. Create SOAP Envelope strSoapEnvelope like this:
    C#
    strSoapEnvelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
    strSoapEnvelope += "<soap:Envelope ";
    strSoapEnvelope += "xmlns:xsi = \"http://www.w3.org/2001/XMLSchema-instance\" ";
    strSoapEnvelope += "xmlns:xsd= \"http://www.w3.org/2001/XMLSchema\" ";
    strSoapEnvelope += "xmlns:soap= \"http://schemas.xmlsoap.org/soap/envelope/\">";
    strSoapEnvelope += "<soap:Body>";
    strSoapEnvelope += "<AddTwoNumbers  
    	xmlns=\"http://localhost/wwwroot/addnumbers/Service1\">";
    strSoapEnvelope +="<a>10</a>";
    strSoapEnvelope += "<b>12</b>"
    strSoapEnvelope += "</AddTwoNumbers >";
    strSoapEnvelope += "</soap:Body>";
    strSoapEnvelope += "</soap:Envelope>";
  7. Create an instance of ServerXMLHTTP40.
    C#
    objXMLHttp = new ServerXMLHTTP40();
  8. Set the header of SOAP request.
    C#
    objXMLHttp.open("POST", http://192.168.84.90/AddNumbers/Service1.asmx,false,"","")
    objXMLHttp.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
    objXMLHttp.setRequestHeader("SOAPAction", 
    	"http://localhost/wwwroot/addnumbers/Service1/AddTwoNumbers");
  9. Send the SOAP request.
    C#
    objXMLHttp.send(strSoapEnvelope.ToString());
  10. Wait for some time.
    C#
    objXMLHttp.waitForResponse(500);
  11. Take response in string outXML.
    C#
    string outXML = objXMLHttp.responseText.ToString();
  12. Display result in label(ID= label1):
    C#
    Label1.Text = outXML.ToString();

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralCall SOAP service over the HTTPS Pin
anrorathod2-Oct-13 23:15
anrorathod2-Oct-13 23:15 
QuestionSwA with MIME Pin
sharath.molagavalli23-Jul-13 7:42
sharath.molagavalli23-Jul-13 7:42 
GeneralWorks great! Pin
jasonee15-Mar-11 4:59
jasonee15-Mar-11 4:59 
GeneralMy vote of 5 Pin
Mickey-MinThein26-Jul-10 21:59
professionalMickey-MinThein26-Jul-10 21:59 
GeneralMy vote of 1 Pin
Tawani Anyangwe27-Apr-09 9:26
Tawani Anyangwe27-Apr-09 9:26 
GeneralMy vote of 1 Pin
Fuzzychaos23-Mar-09 3:48
Fuzzychaos23-Mar-09 3:48 
GeneralMy vote of 1 Pin
virusstorm3-Mar-09 4:41
virusstorm3-Mar-09 4:41 
GeneralMy vote of 1 Pin
Dave Sexton20-Feb-09 4:42
Dave Sexton20-Feb-09 4:42 
Badly formatted, almost unreadable, have no idea what he's actually trying to do here.
Questionerror Pin
aminbeheshti4-Feb-09 19:10
aminbeheshti4-Feb-09 19:10 
GeneralSystem.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction. Pin
Anujit17-Nov-08 18:18
Anujit17-Nov-08 18:18 
GeneralSOAP request to a webservice Pin
webmavin1-May-07 1:41
webmavin1-May-07 1:41 
GeneralRe: SOAP request to a webservice Pin
RedDiamonds22-Jan-09 21:57
RedDiamonds22-Jan-09 21:57 
GeneralGreat Article Pin
Satisht75-Mar-07 15:59
Satisht75-Mar-07 15:59 
QuestionSystem.Web.Services.Protocols.SoapException: Server did not recognize the value of HTTP Header SOAPAction. Pin
sunil0474919-Dec-06 2:08
sunil0474919-Dec-06 2:08 
GeneralNice work... but.... Pin
pinoyBug25-Feb-05 23:17
pinoyBug25-Feb-05 23:17 
GeneralRe: Nice work... but.... Pin
Vimal Kamothi28-Feb-05 16:41
Vimal Kamothi28-Feb-05 16:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.