XML SOAP

The Server

Our server needs to be configured to accept the HTTP post sent by the client. It will be noted that we direct the client’s post to a URL on our local server – http://localhost/soap.asp. So our first job is to create this page, to listen for, and process, SOAP calls to our server. We know that our ASP will receive an XML document, in the form of an HTTP post, with a method name (GetSalesTax), and a parameter (SalesTotal). So, to write our basic listener service, all we need do is deconstruct the body of the request (the SOAP envelope) and pull out the value of the SalesTotal parameter.

The SOAP envelope posted by the client is contained in the body of the request, or in ASP terms the Request object; and because it is XML, we can load it into an instance of Microsoft’s XMLDOM in our ASP. Soap.asp begins like this:

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

objReq.Load Request

Thus objReq contains the SOAP envelope we created on the client, and we can extract the value of the SalesTotal parameter by running an XSL pattern query, using the SelectSingleNode method of the XML DOM object :

strQuery = "SOAP:Envelope/SOAP:Body/m:GetSalesTax/SalesTotal" varSalesTotal = objReq.SelectSingleNode(strQuery).Text

With the parameter extracted, we can make our calculation to get the sales-tax:

varSalesTax = varSalesTotal * 0.04

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.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” - Donald Knuth