Solved

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

Posted on 2011-02-16
6
253 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:andressk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 34913065
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.
0
 

Author Comment

by:andressk
ID: 34918504
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
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 34920240
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:andressk
ID: 34920631
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
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 34921149
Sorry, I'm confused.  The WScript.Echo statement will show a message box on the screen.  Where do you actually want the message to be displayed?

If you *do* want it as a message box on screen, then this will show a message whether it's OK or not.

Regards,

Rob.
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
	WScript.Echo 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
	WScript.Echo FormatDateTime(Now) & " - WS Error " & Err.Number & ": " & Err.Description
	set objShell = Nothing
	set ws = Nothing
	WScript.Quit(4)

End If

Open in new window

0
 

Author Closing Comment

by:andressk
ID: 34922660
Thanks for your help
0

Featured Post

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question