I've written a small VBS script to GET or POST an URL to a WinHTTP object and put its plain-text output to stdout -- any body text it needs should be send via stdin. Please note that it's a stand-alone VBS file, so there's no web involved whatsoever -- it's simply invoked with CSCRIPT SENDURL.VBS GET/POST <url> at a Command Prompt -- SENDURL.VBS is my script.
The problem is that if it times out, that causes an error, and the WinHTTP object's properties such as ResponseText all become invalid, so I want to have a wrapper class which captures the WInHTTP object's OnError event and handles it more gracefully. I have an XLSM which does all this with a wrapper class handling the events, and it works great, but I can't get the class I make in SENDURL.VBS to react to any events. How is this done?
I've Googled this for days, and all I see is stuff about some function called GetRef, which gets a pointer to a function, so I would do something like
Here's the complete class. How can I get event handlers to work when the events happen? Many thanks!
The problem is that if it times out, that causes an error, and the WinHTTP object's properties such as ResponseText all become invalid, so I want to have a wrapper class which captures the WInHTTP object's OnError event and handles it more gracefully. I have an XLSM which does all this with a wrapper class handling the events, and it works great, but I can't get the class I make in SENDURL.VBS to react to any events. How is this done?
I've Googled this for days, and all I see is stuff about some function called GetRef, which gets a pointer to a function, so I would do something like
objWinHTTP.OnError = GetRef(<myErrorHandler>)
, but this only works in the context of a web page, not via the Command Prompt and CSCRIPT. I also read about WScript.ConnectObject, but that doesn't work either.Here's the complete class. How can I get event handlers to work when the events happen? Many thanks!
VBA Code:
class clsWebRequestor
public mWinHTTPrequest ' the WinHTTP object
public BegStatus ' Status at start
public BegContentType ' Content type at start
public ErrNumber ' Error number on error
public ErrDescription ' Error description on error
public Started, Finished
''' CLASS EVENTS
Private Sub Class_Initialize()
Set mWinHTTPrequest = WScript.CreateObject("WinHttp.WinHttpRequest.5.1", "mWinHTTPrequest_")
WScript.ConnectObject mWinHTTPrequest, "mWinHTTPrequest_"
mWinHTTPrequest.SetAutoLogonPolicy AutoLogonPolicy_Always
mWinHTTPrequest.SetTimeouts 300000,300000,300000,300000 ' 300ms = 5 minutes
BegStatus = 0 ' These really just establish data type
BegContentType = ""
ErrNumber = 0
ErrDescription = ""
Started = False
Finished = False
with mWinHTTPrequest ' Set up member event handlers
set .OnError = mWinHTTPrequest_OnError
set .OnResponseStart = mWinHTTPrequest_OnResponseStart
set .OnResponseFinished = mWinHTTPrequest_OnResponseFinished
end with
End Sub
Private Sub Class_Terminate()
if not (mWinHTTPrequest is nothing) then mWinHTTPrequest.Abort
Set mWinHTTPrequest = Nothing
End Sub
''' WINHTTP EVENTS
Private Sub mWinHTTPrequest_OnResponseStart(ByVal Status, ByVal ContentType) ' long and string
BegStatus = Status
BegContentType = ContentType
ErrNumber = 0
ErrDescription = ""
Started = True
Finished = False
End Sub
Private Sub mWinHTTPrequest_OnError(ByVal ErrorNumber, ByVal ErrorDescription) ' long and string
ErrNumber = ErrorNumber
ErrDescription = ErrorDescription
Started = False
Finished = True
End Sub
Private Sub mWinHTTPrequest_OnResponseFinished()
Started = False
Finished = True
End Sub
End Class