perfectxml.com
 Basic Search  Advanced Search   
Topics Resources Free Library Software XML News About Us
  You are here: home Info Bank Articles » SOAP is really Simple Saturday, 23 February 2008
 

Back to Articles Page      

        

SOAP is really Simple


Table of contents
Introduction
About Sample Application
The Client
The Server
Summary

Download source code for sample application

  • Introduction
    I still remember the day when I wrote my first DCOM (Distributed COM)What is DCOM? sample application, few years ago. I was really thrilled to see how simple it was to write an distributed application. The code was straightforward, but configuration needed some work (using DCOMCNFG.exe). Also, this DCOM application was not firewall friendly. Not just DCOM, but other Distributed Computing specifications (like CORBA, RMI, etc.) also have issues and complexities when firewalls are involved. What we need is really simple, solid protocol to facilitate distributed computing. It should be easy for the client to make calls to objects running on the server (a different machine, possibly on the internet and across the firewall boundaries) and get the results. Enter SOAP! A W3C specification based on two solid proven technologies (XML and HTTP).

    SOAP, or Simple Object Access Protocol is an XML-based object invocation protocol. SOAP was originally developed for distributed applications to communicate over HTTP and through corporate firewalls. SOAP defines the use of XML and HTTP to access services, objects and servers in a platform-independent manner.

    In this article, we'll learn how easy it is to write SOAP Web service and a client, that can communicate over internet without any firewall issues. Our server or Web service is an ASP Page and client is an Visual Basic 6.0 application.


  • About Sample Application
    Our client is an Visual Basic 6.0 Application, with a single form having two edit boxes to accept username and password. Here is how Visual Basic client looks like:
    Client Visual Basic 6.0 Application

    When clicked on Connect button, a method on the server (which is an ASP Page) is called which in turn returns result indicating successful or incorrect login, depending on input parameters with the method call. Visual Basic application gets the results and just pops up a message box indicating login success or failure.

    To call a method on the server, we build a XML packet (document/fragment) and post it to the server. The server (ASP Page) processes the input request, generates the result, again as an XML document and sends back to client as response. Please note that the username and password are hard-coded in the ASP page server code as PerfectXML and XML respectively (case-sensitive).


  • The Client
    The Visual Basic client accepts the username and password and calls DoLogin method on the server, passing username & password as method parameters. With SOAP, method call and input parameters needs to be XML-encoded, as below:

    <DoLogin>
       <UserName>PerfectXML</UserName>
       <Password>XML</Password>
    </DoLogin>


    According to SOAP specification, the above method call information needs to be enclosed in what is called as SOAP envelop, which is finally sent/posted to the server. The final SOAP request envelop is shown below:

    <SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
       <SOAP:Header></SOAP:Header>
       <SOAP:Body>
         <m:DoLogin xmlns:m="urn:soapserver/soap:AuthorizationModule">
             <UserName>PerfectXML</UserName>
             <Password>XML</Password>
         </m:DoLogin>
       </SOAP:Body>
    </SOAP:Envelope>


    The Visual Basic client application simply builds the above method call SOAP envelop (as string) and now in order to post this to the server, we use XMLHTTPRequest object from MSXML. Here is the part of code which builds SOAP request envelop and sends it to the server using XMLHTTPRequest:

    Dim objXMLHTTP As New MSXML.XMLHTTPRequest
    Dim objOutputXMLDoc As New MSXML.DOMDocument

    Dim strMethodPkg As String
    Dim strMethodResultXML As String

    Dim iLoginResult As Integer

    strMethodPkg = "<SOAP:Envelope xmlns:SOAP=""http://schemas.xmlsoap.org/soap/envelope/"">"
    strMethodPkg = strMethodPkg & "<SOAP:Header></SOAP:Header>"
    strMethodPkg = strMethodPkg & "<SOAP:Body><m:DoLogin xmlns:m=""urn:soapserver/soap:AuthorizationModule"">"
    strMethodPkg = strMethodPkg & "<UserName>" & txtUserName.Text & "</UserName>"
    strMethodPkg = strMethodPkg & "<Password>" & txtPassword.Text & "</Password>"
    strMethodPkg = strMethodPkg & "</m:DoLogin></SOAP:Body></SOAP:Envelope>"

    objXMLHTTP.open "post", "http://127.0.0.1/soap/simplesoap.asp", False

    objXMLHTTP.setRequestHeader "Content-Type", "text/xml"

    objXMLHTTP.setRequestHeader "SOAPAction", "soapserver/soap:AuthorizationModule#DoLogin"

    objXMLHTTP.send strMethodPkg

    As mentioned earlier, the server for our sample application is an ASP Page. This ASP page is named simplesoap.asp and should be present in a virtual directory named soap. (You can keep the ASP page wherever you wish, just change the URL in the above client code.).

    Now let's have a look at our server application, simplesoap.asp.


  • The Server
    The server ASP page is very simple to understand - it uses MSXML DOM to get the username and password values from the input request XML document, maches those values, builds the result XML document/envelop and sends the response back to the client.

    Set objXMLDOM = Server.CreateObject("Microsoft.XMLDOM")

    objXMLDOM.load Request

    varUserName = objXMLDOM.selectSingleNode("SOAP:Envelope/SOAP:Body/m:DoLogin/UserName").Text
    varPassword = objXMLDOM.selectSingleNode("SOAP:Envelope/SOAP:Body/m:DoLogin/Password").Text

    if varUserName = "PerfectXML" and varPassword = "XML" then
    strResult = 1
    else
    strResult = -1
    end if

    strResultXML = "<SOAP:Envelope xmlns:SOAP=""http://schemas.xmlsoap.org/soap/envelope/""><SOAP:Header></SOAP:Header>"
    strResultXML = strResultXML & "<SOAP:Body><m:DoLoginResponse xmlns:m=""urn:soapserver/soap:AuthorizationModule"">"
    strResultXML = strResultXML & "<LoginResult>" & strResult & "</LoginResult></m:DoLoginResponse>"
    strResultXML = strResultXML & "</SOAP:Body></SOAP:Envelope>"

    Response.Write strResultXML

    The client receives the response. And since response from ASP page (method call) is again an XML document, the VB client app, loads the document and queries for result node's value. Depending on the result value, it then pops up either "login successful" or "login failure" message box. Here is rest of VB client code:
    strMethodResultXML = objXMLHTTP.responseText

    objOutputXMLDoc.loadXML strMethodResultXML

    iLoginResult = objOutputXMLDoc.selectSingleNode("SOAP:Envelope/SOAP:Body/m:DoLoginResponse/LoginResult").Text

    If iLoginResult > 0 Then
    MsgBox "Login Successful"
    Else
    MsgBox "Login Failed!"
    End If




  • Summary
    SOAP really is a way to build distributed application which are simple, extensible, and platform-independent. In this small article, we learned how SOAP works using a Visual Basic client application connecting to ASP page, acting as a server.

    If you would like to learn more about SOAP or have any question about SOAP, check out perfectxml.com's exclusive section dedicated to SOAP. We'll continue to grow the SOAP resource center on perfectxml.com. If you have any ideas/suggestions/comments, please direct them to comments@perfectxml.com.


What is DCOM?DCOM or Distributed COM is Microsoft's distributed computing protocol and is built on top of DCE RPC, an RPC protocol developed by the Distributed Computing Environment consortium and adopted and extended slightly by Microsoft to support communication between COM objects across machines. One of the main design goals of DCOM is location transparency - a client program invoking a remote procedure should use the exact same syntax used for local procedure calls.

  

Back to Articles Page      

All information on this site is for training only. We do not warrant its correctness or its fitness to be used. The risk of using it remains entirely with the user.

 

  Contact Us | E-mail Us | Site Guide | About PerfectXML | Advertise ©2004 perfectxml.com. All rights reserved. | Privacy