Calling WCF service by VBScript

Don’t use MSSOAP. I think it is out of support now, for the past 3 or 4 years. Consider using the XmlHttp, which is part of MSXML, and is supported and continues to be maintained. You will have to construct a SOAP envelope manually. But it’s more reliable this way.

example code

' URL to the WCF service'
url= "http://server:port/Wcf.Service.Address"

Dim requestDoc
Set requestDoc = WScript.CreateObject("MSXML2.DOMDocument.6.0")

Dim root
Set root = requestDoc.createNode(1, "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
requestDoc.appendChild root

Dim nodeBody
Set nodeBody = requestDoc.createNode(1, "Body", "http://schemas.xmlsoap.org/soap/envelope/")
root.appendChild nodeBody

Dim nodeOp
Set nodeOp = requestDoc.createNode(1, "Register", "urn:Your.Namespace.Here")
nodeBody.appendChild nodeOp

Dim nodeRequest
Set nodeRequest = requestDoc.createNode(1, "request", "urn:Your.Namespace.Here")
'content of the request will vary depending on the WCF Service.'
' This one takes just a plain string. '
nodeRequest.text = "Hello from a VBScript client."

nodeOp.appendChild nodeRequest

Set nodeRequest = Nothing
Set nodeOp = Nothing
Set nodeBody = Nothing
Set root = Nothing


'the request will look like this:'
'       <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> '
'         <s:Body> '
'           <Register xmlns="urn:Your.Namespace.Here"> '
'               <request>hello from a VBScript client.</request> '
'           </Register> '
'         </s:Body> '
'       </s:Envelope>'


WSCript.Echo  "sending request " & vbcrlf & requestDoc.xml


dim xmlhttp

set xmlhttp = WScript.CreateObject("MSXML2.ServerXMLHTTP.6.0")
' set the proxy as necessary and desired '
xmlhttp.setProxy 2, "http://localhost:8888"
xmlhttp.Open "POST", url, False
xmlhttp.setRequestHeader "Content-Type", "text/xml"
' set SOAPAction as appropriate for the operation '
xmlhttp.setRequestHeader "SOAPAction", "urn:Set.As.Appropriate"
xmlhttp.send requestDoc.xml

WScript.Echo vbcrlf & "Raw XML response:" & vbcrlf 
WSCript.Echo  xmlhttp.responseXML.xml

dim response
set response= xmlhttp.responseXML
'the response is an MSXML2.DOMDocument.6.0' 
'party on the response here - XPath, walk the DOM, etc. '

FYI: See which-version-of-msxml-should-i-use to learn how to select a version of MSXML.

Leave a Comment