Browse DevX
Sign up for e-mail newsletters from DevX


Simple Object Access Protocol: A Step-By-Step Approach : Page 2

SOAP—Simple Object Access Protocol—is the hottest thing in XML development right now. It has a major role to play in Microsoft's next generation of Visual Studio, and it is the basis of their .NET strategy. If you're looking to write a bullet-proof SOAP service now, in VB6, then look no further than Microsoft's SOAP toolkit for VB; but if you really want to understand what SOAP is all about, then you need to get under the hood, and start building your own objects around the SOAP standard. This article aims to get you started.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Return the Value
Now we have the return value for sales-tax—the response—ready to pass back to the client, but as with the request, we need to format this response correctly, in order to comply with the SOAP standard. The SOAP response envelope conforms to a format-type identical to the request. The only difference is that the "IN" parameter (SalesTotal, in our case) is replaced by an OUT parameter—SalesTax, and the method name indicates that the document is a response:

<SOAP:Envelope xmlns:SOAP="urn:schemas-xmlsoap-org:soap.v1"> <SOAP:Header></SOAP:Header> <SOAP:Body> <m:GetSalesTaxResponse xmlns:m="urn:myserver/soap:TaxCalc"> <SalesTax>4</SalesTax> </m:GetSalesTaxResponse> </SOAP:Body> </SOAP:Envelope>

We can build up this document, either by string-concatenation, or by creating a new instance of a DOM, and appending the appropriate nodes.

Back on the client, the response is received, and can be decoded by extracting the appropriate node from the Envelope document:

Dim objReturn As New MSXML.DomDocument objReturn.LoadXML strReturn strQuery = "SOAP:Envelope/SOAP:Body/m:GetSalesTaxResponse/SalesTax" dblTax = objReturn.SelectSingleNode(strQuery).Text

And that’s it! A functional, compliant SOAP service in a few easy steps. Of course, the service that we have provided is far from sophisticated, but that is to miss the point. In the not-too-distant future, Visual Studio 7 will completely mask the implementation of SOAP; but I believe that there is value in understanding the way the engine turns beneath the hood. SOAP itself is a very simple protocol; and I hope you leave this article with a better understanding the infrastructure that lies behind SOAP, and the methodology through which SOAP-based component services are going to be provided in the future.

Appendix 1: VB Client Code

NB—This project contains a single module, with a Sub Main start-up object, and references the MSXML library. The code assumes you are running a local web server (localhost), and that soap.asp is located in the web root, as described below.

Sub Main() Dim objHTTP As New MSXML.XMLHTTPRequest Dim strEnvelope As String Dim strReturn As String Dim objReturn As New MSXML.DOMDocument Dim dblTax As Double Dim strQuery As String 'Create the SOAP Envelope strEnvelope = _ "<SOAP:Envelope xmlns:SOAP=""urn:schemas-xmlsoap-org:soap.v1"">" & _ "<SOAP:Header></SOAP:Header>" & _ "<SOAP:Body>" & _ "<m:GetSalesTax xmlns:m=""urn:myserver/soap:TaxCalculator"">" & _ "<SalesTotal>100</SalesTotal>" & _ "</m:GetSalesTax>" & _ "</SOAP:Body>" & _ "</SOAP:Envelope>" 'Set up to post to our local server objHTTP.open "post", "http://localhost/soap.asp", False 'Set a standard SOAP/ XML header for the content-type objHTTP.setRequestHeader "Content-Type", "text/xml" 'Set a header for the method to be called objHTTP.setRequestHeader "SOAPMethodName", _ "urn:myserver/soap:TaxCalculator#GetSalesTax" 'Make the SOAP call objHTTP.send strEnvelope 'Get the return envelope strReturn = objHTTP.responseText 'Load the return envelope into a DOM objReturn.loadXML strReturn 'Query the return envelope strQuery = _ "SOAP:Envelope/SOAP:Body/m:GetSalesTaxResponse/SalesTax" dblTax = objReturn.selectSingleNode(strQuery).Text Debug.Print dblTax End Sub

Appendix 2: ASP Server Code

This code resides in an ASP—Soap.asp, in the root directory of the web server.

<% Set objReq = Server.CreateObject("Microsoft.XMLDOM") 'Load the request into XML DOM objReq.Load Request 'Query the DOM for the input parameter strQuery = "SOAP:Envelope/SOAP:Body/m:GetSalesTax/SalesTotal" varSalesTotal = objReq.SelectSingleNode(strQuery).Text 'Calculate the sales tax varSalesTax = varSalesTotal * 0.04 'Prepare the return envelope strTmp = _ "<SOAP:Envelope xmlns:SOAP=""urn:schemas-xmlsoap-org:soap.v1"">" & _ "<SOAP:Header></SOAP:Header>" & _ "<SOAP:Body>" & _ "<m:GetSalesTaxResponse xmlns:m=""urn:myserver/soap:TaxCalc"">" & _ "<SalesTax>" & varSalesTax & "</SalesTax>" & _ "</m:GetSalesTaxResponse>" & _ "</SOAP:Body>" & _ "</SOAP:Envelope>" 'Write the return envelope Response.Write strTmp %>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date