Solved

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

Posted on 2011-02-16
6
252 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
Industry Leaders: 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

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!

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

732 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