Link to home
Start Free TrialLog in
Avatar of andressk
andresskFlag for Colombia

asked on

Script to Test a Web Service and Returns the Response on a Message

Hi,
I´m working on a script to consult a web service, execute a method and returns the response.
It is working fine and I´m loading it via WMI to the remote server. It writes the response on the event log of the server. But now I need that it returns the response on a message and that if the method is not available, returns an exit with an error code. Like "Wscript.Quit(4)"
How can I do that?
Thanks
This is the script:
 
Option Explicit
class WebService
  public Url
  public Method
  public Response
  public Parameters
 
 'Function to invoke the Web Service
  public function execute()
    dim xmlhttp
    Set xmlhttp = CreateObject("Microsoft.XMLHTTP")
    xmlhttp.open "POST", Url & "/" & Method, false
    xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"	
    xmlhttp.send Parameters.toString
    response = xmlhttp.responseText
    set xmlhttp = nothing
  end function
  
  Private Sub Class_Initialize()
    Set Parameters = new wsParameters
  End Sub
  
  Private Sub Class_Terminate()
    Set Parameters = Nothing
  End Sub
End class
 
'**************************************************** 
' Class wsParameters
'****************************************************

class wsParameters
  public mCol
  public function toString()
    dim nItem
    dim buffer
    buffer = ""
    for nItem = 1 to Count
      buffer = buffer & Item(nItem).toString & "&"
    next
    if right(buffer,1)="&" then
      buffer = left(buffer,len(buffer)-1)
    end if
    toString = buffer 
  end function
  public sub Clear
    set mcol = nothing 
    Set mCol = CreateObject("Scripting.Dictionary") 
  end sub
  public sub Add(pKey,pValue)
    dim newParameter
  
    set newParameter = new wsParameter
    newParameter.Key = pKey
    newParameter.Value = pValue
    mCol.Add mCol.count+1, newParameter
  
    set newParameter = nothing
  end sub
  public function Item(nKey)
    set Item=mCol.Item(nKey)
  end function
  public function ExistsXKey(pKey)
    dim nItem
  
    for nItem = 1 to mcol.count
      if mCol.Item(nItem).key = pKey then
        ExistsXKeyword = true
        exit for
      end if
    next
  end function
  public sub Remove(nKey)
    mCol.Remove(nKey)
  end sub
  public function Count()
    Count=mCol.count
  end function
  Private Sub Class_Initialize()
    Set mCol = CreateObject("Scripting.Dictionary")
  End Sub
  Private Sub Class_Terminate()
    Set mCol = Nothing
  End Sub
end class
 
'**************************************************** 
' Class wsParameter
'****************************************************
 
class wsParameter
   public Key
   public Value
   public function toString()
     toString = Key & "=" & Value
   end function
end class

dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
set objShell = Nothing
set ws = Nothing
WScript.Quit

Open in new window

Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, maybe change this section:

 
dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

Const EVENT_SUCCESS = 0
Set objShell = Wscript.CreateObject("Wscript.Shell")
objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
set objShell = Nothing
set ws = Nothing
WScript.Quit

Open in new window


to this:

 
On Error Resume Next
dim ws, objShell
 
'Creates the object of the Web Service 
set ws = new webservice
ws.Url = "http://xxxxxxxxxxxxxxxx.asmx"
'Calls the method
ws.Method = "xxxxx"
'Fill consult parameters
ws.Parameters.Add "login", "1234390p"
ws.Parameters.Add "password","temp"
 
ws.execute

If Err.Number = 0 Then

	Const EVENT_SUCCESS = 0
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_SUCCESS, FormatDateTime(Now) & " - WS is OK " & ws.Response
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit

Else

	Const EVENT_ERROR = 1
	Set objShell = Wscript.CreateObject("Wscript.Shell")
	objShell.LogEvent EVENT_ERROR, FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit(4)

End If

Open in new window


Regards,

Rob.
Avatar of andressk

ASKER

Hi Rob,

Thanks for your reply, I´ve tested the script and it works fine, it shows the error as expected. One last question is how can I add the ws.Response as a message to show the WS response without checking the windows log?

Regards
If you want to show the message to screen, then under this:
      objShell.LogEvent EVENT_ERROR, FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description

you can add this:
      WScript.Echo FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description

But if you run the script as a scheduled task, it won't show up....it only works if you run it manually.

Regards,

Rob.
Hi Rob,

I´ve made the suggested change but I can´t get the message with the Response, not as a window that shows up, but something like:
Wscript.Echo "Message: Response " & FormatDateTime(Now) & ws.Response
Thanks
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for your help