Simple WebService Data Consumer (Geolocation).
'//////////////////////////////////////////////////////////////////////////////
' Author: SYO
' Copyright (c) 2012
' ScriptEZ.API based
application which uses facilities from RAD API:
' . Demoing a "Web Service Consumer"
' . Echo() method
usage which displays always messages into a console window
' instead of WScript.Echo() with 2 behaviours for cscript.exe
or wscript.exe
' . ScriptEZ.API Remoting Control Interface usage by NPTelnet.exe
program
' (Connect to
ScriptEZ.API process with <processid (PID)> as connection name)
'
' Purpose: Request
for a Geolocalisation from a Web Service located at:
' http://www.webservicex.net/geoipservice.asmx/GetGeoIPContext?
'//////////////////////////////////////////////////////////////////////////////
' These below consts can
be modified
' --begin
const METHOD = "POST" ' "POST"
or "GET"
const HEADER = "0" ' size of header
area in byte
const URL =
"http://www.webservicex.net"
' POST method
const POST_VARS = "" ' Each variable
must be encoded with URLEncode()
const RAW_REMOTEFILENAME = "/geoipservice.asmx/GetGeoIPContext"
' GET method
const REMOTEFILENAME = "/geoipservice.asmx/GetGeoIPContext?"
' --end
const SCRIPTEZ_API = "ScriptEZ.API" '
Component name
Dim ScriptEZ
' start the script
'/////////////////
Main()
' Main
Subroutine
'////////////////
Sub
Set WshShell = CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
Dim Windir
Dim FullQualifiedEngineName
'-- if Win64,
restart with WScript.exe/CScript.exe 32 bits
On Error Resume Next
Windir = WshShell.ExpandEnvironmentStrings("%WinDir%")
FullQualifiedEngineName = Windir+"\SysWOW64"
+ Mid(WScript.FullName,InStrRev(WScript.FullName,"\"),Len(WScript.FullName))
If fso.FolderExists(Windir+"\SysWOW64")
And _
LCase(WScript.FullName)
<> LCase(FullQualifiedEngineName) And
_
fso.FileExists(FullQualifiedEngineName)
= True Then
WshShell.Run
FullQualifiedEngineName + " " + Chr(34) + CStr(WScript.ScriptFullName)
+ Chr(34)
Exit Sub
End If
On Error Resume Next
'-- attempt to
instantiate component object with callback interface
Set ScriptEZ = WScript.CreateObject(SCRIPTEZ_API)
If Not IsObject(ScriptEZ)
Then
MsgBox "Please, download
and register " + SCRIPTEZ_API + _
"
component from http://sovann.googlepages.com and try again...",vbInformation
Exit Sub
End If
ScriptEZ.ClearConsole
ScriptEZ.SetCPUCoreAffinity "0"
'-- Register
App for receiving AppEvent(s) with onAppEvent(EventID)
ScriptEZ.AppRegister
Dim readTotalBytes, readBytes, xmlObject
Dim xml_response
readTotalBytes
= 0
xmlObject = 0
'-- endless
loop
Do Until 0
'-- http file
opening and executing
If( METHOD
= "GET") Then
'-- (1) using
GET method for sending request
httpFileHandle
= ScriptEZ.OpenHttpFile(URL,REMOTEFILENAME,CLng(HEADER))
Title = REMOTEFILENAME
ElseIf(METHOD
= "POST") Then
'-- (2) using
POST method for sending request
httpFileHandle
= ScriptEZ.OpenHttpAndPost(URL,RAW_REMOTEFILENAME,POST_VARS,"","",CLng(HEADER))
Title = RAW_REMOTEFILENAME
End If
xml_response = ""
If httpFileHandle > 0 Then
'-- http
file reading
r = ScriptEZ.HttpReadFile(httpFileHandle)
If r = 1 Then
readBytes
= ScriptEZ.HttpGetReadBytes(httpFileHandle)
readTotalBytes
= readTotalBytes + readBytes
'-- if
more than one reading operation, iterate into this loop
Do Until
readBytes <= 0
'-- http
file fetching data
data_chunk = ScriptEZ.HttpGetReadBufferAsBSTR(httpFileHandle)
xml_response = xml_response
+ data_chunk
ScriptEZ.FreeBSTR
data_chunk
If readTotalBytes < 1024
Then
ScriptEZ.SetConsoleTitle
CStr(FormatNumber(readTotalBytes,0,0,-2)) + " Bytes
read from " + _
URL + Title + " by HTTP/" + METHOD
Else
ScriptEZ.SetConsoleTitle
CStr(FormatNumber(readTotalBytes/1024,0,0,-2)) + _
" KBytes (" + CStr(readTotalBytes) + " Bytes) read
from " + _
URL + Title + " by HTTP/" + METHOD
End If
'-- http
file reading
r = ScriptEZ.HttpReadFile(httpFileHandle)
readBytes
= ScriptEZ.HttpGetReadBytes(httpFileHandle)
readTotalBytes
= readTotalBytes + readBytes
End If
'-- http
file closing
ScriptEZ.CloseHttpFile httpFileHandle
End If
If httpFileHandle > 0 Then
ScriptEZ.ClearConsole
'-- displaying
data here....
If xmlObject <= 0 Then
xmlObject
= ScriptEZ.CreateXMLObject(xml_response)
Else
r = ScriptEZ.PopulateXMLObject(xmlObject,xml_response)
End If
IP = ScriptEZ.NamedItemValueFromElement(xmlObject,"IP","<text/>")
CountryName = ScriptEZ.NamedItemValueFromElement(xmlObject,"CountryName","<text/>")
CountryCode = ScriptEZ.NamedItemValueFromElement(xmlObject,"CountryCode","<text/>")
ScriptEZ.Echo vbLF + " . Own Memory in use : " + CStr(FormatNumber(ScriptEZ.ProbeMemoryStatus(7)/1024,2))
+ " KBytes" + vbLF
ScriptEZ.Echo vbLF + " . Your GeoIdentity is"
ScriptEZ.Echo vbLF +
" Public IP..........: " + IP
ScriptEZ.Echo vbLF +
" CountryName........: " + CountryName
+ " (" + CountryCode + ")"
ScriptEZ.FreeBSTR IP
ScriptEZ.FreeBSTR
CountryName
ScriptEZ.FreeBSTR
CountryCode
End If
'-- pause 5
seconds
WScript.Sleep 5000
'-- App
unregister before ending
ScriptEZ.AppUnregister
Set ScriptEZ = Nothing
End Sub
Function GetMyPublicIP()
'-- using GET
method for sending request
local_httpFileHandle = ScriptEZ.OpenHttpFile("http://bot.whatismyipaddress.com","",0)
response
= "n.a"
If local_httpFileHandle > 0
Then
'-- http
file reading
r = ScriptEZ.HttpReadFile(local_httpFileHandle)
If r = 1 Then
read
= ScriptEZ.HttpGetReadBytes(local_httpFileHandle)
'-- http
file fetching ip string as xxx.xxx.xxx.xxx
data_chunk = ScriptEZ.HttpGetReadBufferAsBSTR(local_httpFileHandle)
response
= data_chunk
ScriptEZ.FreeBSTR
data_chunk
End If
'-- http
file closing
ScriptEZ.CloseHttpFile
local_httpFileHandle
End If
GetMyPublicIP = response
End Function