In VBScript via CSCRIPT only, no web involved: how to handle a WinHTTP object's events?

jjasmith4

Board Regular
Joined
Aug 22, 2018
Messages
59
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 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
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top