An Introduction To XML SOAP Using ASP and VB6 - The Server (Page 3 of 5 )
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:
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:
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.