We had a meeting today with some great guys with an even greater service that we’re going to be integrating with our online accounting software.
Their service is exposed as a .Net SOAP webservice. Now you’d think that wouldn’t be a problem for us as our own accounting API uses the same technology. Well, you’d be wrong. Our core product is still coded in classic ASP / VBScript.

So the challenge was to talk to a SOAP webservice from VBScript.  Google threw up a few results, none of them exactly what I needed. The closest was a blog post by Ken Hughes. So I took his approach and re-wrote it for my needs. It’s not the prettiest piece of code I’ve ever written, and would certainly be nicer as a class. But it does the job.

Enough talk, here are the goods. First an include file with all the functions, etc:
Download: _consumewebservice.asp

 
<%
SOAP_ENDPOINT = ""
SOAP_NS = ""
SOAP_FUNCTION = ""
SOAP_REQUEST = ""
SOAP_RESPONSE = ""
function SOAP_StartRequest(sEndPoint, sNameSpace, sFunction)
	SOAP_ENDPOINT = sEndPoint
	SOAP_NS = sNameSpace
	SOAP_FUNCTION = sFunction
	'do the SOAP envelope
	SOAP_REQUEST = ""
    SOAP_REQUEST = SOAP_REQUEST + " "
    'start the SOAP body
    SOAP_REQUEST = SOAP_REQUEST + ""
	'start function
	SOAP_REQUEST = SOAP_REQUEST + ""
end function
Function SOAP_AddParameter(byval strParam, byval strValue)
    Dim strSoap
    SOAP_REQUEST = SOAP_REQUEST + ""
    SOAP_REQUEST = SOAP_REQUEST + strValue
    SOAP_REQUEST = SOAP_REQUEST + ""
End Function
function SOAP_SendRequest()
	'end function, body and envelope
	SOAP_REQUEST = SOAP_REQUEST + ""
	SOAP_REQUEST = SOAP_REQUEST +  ""
    SOAP_REQUEST = SOAP_REQUEST +  ""
	Dim oHttp
	Dim strResult
	Set oHttp = CreateObject("Msxml2.XMLHTTP")
	oHttp.open "POST", SOAP_ENDPOINT, false
	oHttp.setRequestHeader "Content-Type", "text/xml"
	oHttp.setRequestHeader "SOAPAction", SOAP_NS + "/" & SOAP_FUNCTION
	oHttp.send SOAP_REQUEST
	SOAP_RESPONSE =   oHttp.responseText
end function
Function SOAP_GetResult(resultParam)
    Dim oXml
    Set oXml = CreateObject("Msxml2.DOMDocument")
    oXml.Async = true
    oXml.LoadXml SOAP_RESPONSE

    Dim strPath
    strPath = "/*/*//" + resultParam
    Dim oNode
    Set oNode = oXml.documentElement.SelectSingleNode(strPath)
    SOAP_GetResult = oNode.Text
End Function 
%>

And now a quick demo. This connects to our accounting API, calls the GetInvoice function and gives you a couple of the return values.
Download: tryit.asp

 
<!--#include virtual="_consumeWebservice.asp"-->
<%
SOAP_StartRequest "https://secure.kashflow.co.uk/api/service.asmx", "KashFlow", "GetInvoice"
SOAP_AddParameter "UserName", "yourusername"
SOAP_AddParameter "Password", "yourpassword"
SOAP_AddParameter "InvoiceNumber", "37"
SOAP_SendRequest

%>
<html>
<head>
</head>
<body>
Status: <%=SOAP_GetResult("Status")%><br />
NetAmount: <%=SOAP_GetResult("NetAmount")%><br />
VATAmount: <%=SOAP_GetResult("VATAmount")%><br />
</body>
</html>

There are some limitations – it doesn’t handle complex types in the request. But it does what I need it to do and hopefully it’ll be of use to someone else out there.