Consuming a .Net SOAP Webservice from Classic ASP (VBScript)

9

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

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




Status: 
NetAmount: 
VATAmount: 

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.

9 Responses to Consuming a .Net SOAP Webservice from Classic ASP (VBScript)

  1. Klint

    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. 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. 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. Pingback: The KashFlow SaaS Accounting Blog - Sending a Tweet via MS SQL Server KashFlow Accounting Software

  5. Karl

    Thanks for this generic code. Works great and saved me a ton of time! I’m using it to schedule several processes that I need to run in the Task Scheduler. Thanks for posting.

  6. Pretty useful information.. that really works.. Thanks a lot for sharing your knowledge.. ;)

  7. One question: when I use this function, accented letters become “??”. Any idea to solve that? thanks

  8. Well, I solved the problem with the accents. Here what need to be changed:

    Just put “; charset=utf-8″ after “text/xml”, in the SOAP_SendRequest() function.

    Bellow is the changed function to accept accents. I hope it help somebody ;)

    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; charset=utf-8″
    oHttp.setRequestHeader “SOAPAction”, SOAP_NS + “/” & SOAP_FUNCTION
    oHttp.send SOAP_REQUEST
    SOAP_RESPONSE = oHttp.responseText
    end function

  9. Duane Jackson - Founder & CEO

    Hi Férnas,

    I was going to suggest setting the encoding/charset.

    Thanks for sharing your solution

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>