Call a WebService from VBScript

While I was writing some code on VBScript to avoid manual tasks that we currently do with a bunch of files, I needed to automatically upload some information extracted from all these files to a SQL database. One way to do it is using a Web Service that receives the data and store it into the database.

Our Web Service is able to receive the following protocols: SOAP 1.1, SOAP 1.2 and HTTP POST. For simplicity, we are going to use HTTP POST, which receive the parameters in the query string format (param1=value1&param2=value2&…). In the other protocols (SOAP) we would need to send the parameters in a XML way (which is more powerful but a little more extensive to implement).

The expected HTTP request will be:

POST /WebService.asmx/WebMethod HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
Content-Length: length

param=string

And the response (in XML format):

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">string</string>

So, to call the Web Service directly from the VBScript, we can use the following code:

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
'The object that will make the call to the WS
Set oXMLHTTP = CreateObject("Microsoft.XMLHTTP")
'The object that will receive the answer from the WS
Set oXMLDoc = CreateObject("Microsoft.XMLDOM")
 
strParam = "string to pass"
 
'Tell the name of the subroutine that will handle the response
oXMLHTTP.onreadystatechange = getRef("HandleStateChange")
'Initializes the request (the last parameter, False in this case, tells if the call is asynchronous or not
oXMLHTTP.open "POST", "http://localhost/WebService.asmx/WebMethod", False
'This is the content type that is expected by the WS using the HTTP POST protocol
oXMLHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
 
'Now we send the request to the WS
oXMLHTTP.send "parameter=" & strParam
 
Sub HandleStateChange()
	Dim szResponse
	'When the call has been completed (ready state 4)
	If oXMLHTTP.readyState = 4 Then
		szResponse = oXMLHTTP.responseText
		oXMLDoc.loadXML szResponse
		'If the WS response is not in XML format, there is a problem
		If oXMLDoc.parseError.errorCode <> 0 Then
			WScript.Echo "ERROR:"
			WScript.Echo oXMLHTTP.responseText
			WScript.Echo oXMLDoc.parseError.reason
		Else
			WScript.Echo "Result: " & oXMLDoc.getElementsByTagName("string")(0).childNodes(0).Text
		End If
	End If
End Sub

The ready state tells us about the connection with the Web Service:

  • 0: Uninitialized – open() has not been called yet.
  • 1: Loading – send() has not been called yet.
  • 2: Loaded – send() has been called, headers and status are available.
  • 3: Interactive – Downloading, responseText holds the partial data.
  • 4: Completed – Finished with all operations.

One thought on “Call a WebService from VBScript”

  1. Hello Soy,

    Thank you for sharing the code.
    Can you please help me on how to upload a file.
    We have two parameters in the web-service (1)File name (2)File bytes

    Regards,
    Bharath

Leave a Reply

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

Are you human? *

This site uses Akismet to reduce spam. Learn how your comment data is processed.