Consuming a .Net SOAP Webservice from Classic ASP (VBScript)
This is probably the most technical post I’ve made to date – so unless the title makes sense to you, the rest of this is likely to be a foreign laguage too.
We had a meeting today with some great guys with an even greater service that we’re going to be integrating with our online bookkeeping software (more on that another time). 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
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.