Forgot your password?

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 accounting 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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<%
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 = "<?xml version=""1.0"" encoding=""utf-8""?>"
    SOAP_REQUEST = SOAP_REQUEST + "<soap12:Envelope "
    SOAP_REQUEST = SOAP_REQUEST + "xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" "
    SOAP_REQUEST = SOAP_REQUEST + "xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" "
    SOAP_REQUEST = SOAP_REQUEST + "xmlns:soap12=""http://www.w3.org/2003/05/soap-envelope""> "
    'start the SOAP body
    SOAP_REQUEST = SOAP_REQUEST + "<soap12:Body>"
	'start function
	SOAP_REQUEST = SOAP_REQUEST + "<" + SOAP_FUNCTION + " xmlns=""" + SOAP_NS + """>"
end function
Function SOAP_AddParameter(byval strParam, byval strValue)
    Dim strSoap
    SOAP_REQUEST = SOAP_REQUEST + "<" + strParam + ">"
    SOAP_REQUEST = SOAP_REQUEST + strValue
    SOAP_REQUEST = SOAP_REQUEST + "</" + strParam + ">"
End Function
function SOAP_SendRequest()
	'end function, body and envelope
	SOAP_REQUEST = SOAP_REQUEST + "</" + SOAP_FUNCTION + ">"
	SOAP_REQUEST = SOAP_REQUEST +  "</soap12:Body>"
    SOAP_REQUEST = SOAP_REQUEST +  "</soap12:Envelope>"
	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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
<!--#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.

[Post to Twitter]  [Post to Delicious]  [Post to Digg]  [Post to StumbleUpon] 

Tags: , ,

This entry was posted on Thursday, November 27th, 2008 at 8:00 am and is filed under Programming, Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Consuming a .Net SOAP Webservice from Classic ASP (VBScript)”

  1. Klint says:

    This post just made my day. Thank you for posting your code for all. It works like a charm, and the code is very logical and easy to understand.

  2. mortimernova says:

    This is probably the most useful SOAP to classic ASP information I’ve found in the last week. Everything I’ve found has been broken and this is likely going to save me hours of more searching. Bravo to you and thanks for posting this up.

  3. Paul wood says:

    SOAP to classic ASP.
    I havent found any other info that worked, and is easy to understand. You have made my day.
    Your my hero of the week.

  4. [...] a side note, I’ve only written one really techie post before on this blog. I’ve deliberately refrained from doing too many as I’m assuming [...]

Leave a Reply


Sage Show-off SaaS Solution – “Sage Live” » « VAT Changes? What Changes?


Awards and stuff