Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2011-02-16
6
Medium Priority
?
255 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 2000 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

Independent Software Vendors: 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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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