AutoIT – Getting data from a Web Service

Web Services are a nice feature on the internet. But accessing it as a scripter is not so obvious.

Let’s see here how it goes.

First we need to have the URL of the Web Service and peak into the SOAP Envelop structure.

http://www.webservicex.net/uszip.asmx

image

This API returns 4 methods to retrieve data back depending on some input.

Let’s take the GetInfoByZIP for Example. You see and input field and a SOAP envelope structure which we need later on.

image

If you enter some ZIP CODE you will get an XML response.

image

But if we want to script is we need to get WSDL definition.

This site offers an online WSDL browser.

http://wsdlbrowser.com

image

Look at the ?WSDL extention after the URL

Now we have everuthin we setup our script to get the data.

image

In the script I added some debug information output.

You can capture the Debug XML response, and add it to an XML Parser for further investigation.

image

Once you know how the XML is formatted, we can capture the data in it.

image

Here is the full code.

Dim $objHTTP
Dim $strEnvelope
Dim $strReturn
Dim $objReturn
Dim $strQuery
Dim $strValue

$strValue = InputBox("Testing", "Enter your new value here.", 60007)

; Initialize COM error handler
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")

$objHTTP = ObjCreate("Microsoft.XMLHTTP")
$objReturn = ObjCreate("Msxml2.DOMDocument.3.0")

; Create the SOAP Envelope
$strEnvelope = '<?xml version="1.0" encoding="utf-8"?>' & _
'<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">' & _
'<soap:Body>' & _
'<GetInfoByZIP xmlns="http://www.webserviceX.NET">' & _
'<USZip>'&$strValue&'</USZip>' & _
'</GetInfoByZIP>' & _
'</soap:Body>' & _
'</soap:Envelope>'

; Set up to post to the server
$objHTTP.open ("post", "http://www.webservicex.net/uszip.asmx?op=GetInfoByZIP", False)

; Set a standard SOAP/ XML header for the content-type
$objHTTP.setRequestHeader ("Content-Type", "text/xml")

; Set a header for the method to be called
$objHTTP.setRequestHeader ("SOAPMethodName", "urn:myserver/soap:TaxCalculator#Getsalestax")

ConsoleWrite("Content of the Soap envelope : "& @CR & $strEnvelope & @CR & @CR)

; Make the SOAP call
$objHTTP.send ($strEnvelope)

; Get the return envelope
$strReturn = $objHTTP.responseText

ConsoleWrite("-----------" & @CRLF)
ConsoleWrite ("Debug Response : "& @CR & $strReturn & @CR & @CR)
ConsoleWrite("-----------" & @CRLF)

; Load the return envelope into a DOM
$objReturn.loadXML ($strReturn)

ConsoleWrite("Return of the SOAP Msg : " & @CR & @CR & $objReturn.XML & @CR & @CR)

; Query the return envelope
$strQuery = "SOAP:Envelope/SOAP:Body"

$objReturn.selectSingleNode($strQuery)

$Soap = $objReturn.Text

MsgBox(0,"SOAP Response","The Response is  : " & $Soap)

Func MyErrFunc()
  $HexNumber=hex($oMyError.number,8)
  Msgbox(0,"COM Test","We intercepted a COM Error !"       & @CRLF  & @CRLF & _
			 "err.description is: "    & @TAB & $oMyError.description    & @CRLF & _
			 "err.windescription:"     & @TAB & $oMyError.windescription & @CRLF & _
			 "err.number is: "         & @TAB & $HexNumber              & @CRLF & _
			 "err.lastdllerror is: "   & @TAB & $oMyError.lastdllerror   & @CRLF & _
			 "err.scriptline is: "     & @TAB & $oMyError.scriptline     & @CRLF & _
			 "err.source is: "         & @TAB & $oMyError.source         & @CRLF & _
			 "err.helpfile is: "       & @TAB & $oMyError.helpfile       & @CRLF & _
			 "err.helpcontext is: "    & @TAB & $oMyError.helpcontext _
			)
  SetError(1)
Endfunc

In Powershell this is only a few lines 2 to be exact.But at least now you know what is going on behind the scene. Smile

See Example here

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: