How to Send SOAP call using MSXML (replace STK)

I know… This is a simple thing, but there is no documentation on it so here it is:

UPDATE:  You can now use Windows Webservica API (WWSAPI) to create native code WebService calls!

The SoapToolkit (STK) is out of support.  So what if you need to make a SOAP call in VB 6 or scripting?  Realizing that SOAP calls to web service are just a POST with some XML, you should be able to do this easily.  You can get the format for this POST from a network sniffer such as Network Monitor (available from Microsoft) or from the WSDL of service definition.

Here is a simple definition of a web service that returns the length of a string:




The test form is only available for requests from the local machine.

SOAP 1.1

The following is a sample SOAP 1.1 request and response. The placeholders shown need to be replaced with actual values.

POST /HelloWS/service.asmx HTTP/1.1
Host: jsandersvm2003
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: ""

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
    <GetStringLength xmlns="">
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="" xmlns:xsd="" xmlns:soap="">
    <GetStringLengthResponse xmlns="">

Here is one way to make this call in a simple VB form application. Reference MSXML3.0 in your project (beware of lines wrapping in your browser if you are a cut and past programmer).







    ‘How to send this using MSXML
    ‘POST /HelloWS/service.asmx HTTP/1.1
    ‘Host: jsandersvm2003
    ‘Content-Type: text/xml; charset=utf-8
    ‘Content -length: <<length>>

    ‘SOAPAction: “”




    ‘<?xml version=”1.0″ encoding=”utf-8″?>
‘<soap:Envelope xmlns:xsi=”” xmlns:xsd=”” xmlns:soap=””>
‘ <soap:Body>
‘ <GetStringLength xmlns=””>
‘ <theString><<string>></theString>
‘ </GetStringLength>
‘ </soap:Body>





    Dim myXML As New





    ‘ False means Synchronously“POST”, “http://jsandersvm2003/HelloWS/service.asmx”, False)
“Content-Type”, “text/xml; charset=utf-8”)

    ‘ NOTE: You could do this… very brute force way to send it.
‘Dim theTxt As String
‘theTxt = “<?xml version=” & Chr(34) & “1.0” & Chr(34) & ” encoding=” & Chr(34) & “utf-8” & Chr(34) & “?><soap:Envelope xmlns:xsi=” & Chr(34) & “” & Chr(34) & ” xmlns:xsd=” & Chr(34) & “” & Chr(34) & ” xmlns:soap=” & Chr(34) & “” & Chr(34) & “><soap:Body><GetStringLength xmlns=” & Chr(34) & “” & Chr(34) & “><theString xmlns=” & Chr(34) & “” & Chr(34) & “>string</theString></GetStringLength></soap:Body></soap:Envelope>”








    ‘myXML.send theTxt

‘ NOTE: This allows you to leverage the XML DOM




    Dim node
Dim xmlSrcDoc As New MSXML2.DOMDocument30
    xmlSrcDoc.async =
xmlSrcDoc.validateOnParse = False
xmlSrcDoc.resolveExternals = False
xmlSrcDoc.preserveWhiteSpace = True
‘ caveat for the createProcessingInstruction. the encoding string will be missing from the XML instruction line.
‘ so you could skip the createProcessingInstruction code, instead prepend the string to the XML you generate for the rest of the body ala 

“myXML.send “<?xml version=” & Chr(34) & “1.0” & Chr(34) & ” encoding=” & Chr(34) & “utf-8″ & Chr(34) & ” ?> ” & xmlSrcDoc.xml




    ‘ most servers will not care about that because the HTTP header already specifies the encoding
‘ Create a processing instruction targeted for xml.
    node = xmlSrcDoc.createProcessingInstruction(
“xml”, “version=’1.0′”)
‘ issue: cannot do this: node.setAttribute “encoding”, “utf-8”
    node =
‘ Create the Soap Envelope
Dim soapEnvelope As IXMLDOMElement
    soapEnvelope = xmlSrcDoc.createElement(
‘ Create the attributes for it
soapEnvelope.setAttribute(“xmlns:soap”, “”)
“xmlns:xsi”, “”)
“xmlns:xsd”, “”)
‘ Create the Soap Body
Dim soapBody As IXMLDOMElement
    soapBody = xmlSrcDoc.createElement(
‘ Create the node for the call we are making
Dim soapCall As IXMLDOMElement
    soapCall = xmlSrcDoc.createElement(
“xmlns”, “”)
‘ finally add the argument(s)
Dim soapArgs As Object
soapArgs = xmlSrcDoc.createElement(“theString”)
    soapArgs.Text =
“Jeff is cool”
‘ let it go!
‘ myXML.send “<?xml version=” & Chr(34) & “1.0” & Chr(34) & ” encoding=” & Chr(34) & “utf-8″ & Chr(34) & ” ?> ” & xmlSrcDoc.xml
Dim xmlDoc As MSXML2.DOMDocument30
    xmlDoc = myXML.responseXML
‘ This query will depend on what you expect back from the SOAP call





    ‘ or this if you want to traverse the XML DOM
‘Dim oNode As Object
‘Set oNode = xmlDoc.documentElement.selectSingleNode(“//HelloWorldResult”)
‘If oNode Is Nothing Then
‘     MsgBox “Requested information not found.”
‘     MsgBox oNode.Text

‘End If






Comments (1)

  1. Yves M. says:

    Hello Jeff

    What can I do when i have my complete XML-File for the insert already?

    Where can i read this from the FS in a variable?

    Greetings Yves