VBScript MSXML2.XMLHTTP.responsetext not updating when called again

6.1k Views Asked by At

I'm trying to develop a script that authenticates itself with a server then loops through getting new data from the server.

Bellow is a the code I'm using

Set s = CreateObject("MSXML2.XMLHTTP")

'Server authentication
s.open "GET", "fooAPI", False
s.send

do

    'Server data
    s.open "GET", "barAPI", False
    s.send
    Wscript.echo s.responseText
    WScript.Sleep 5000
loop

The first time it runs I get correct up to date info but once it loops it gives me the same info from the first run

The closest I could find was this But it was left unanswered.

I was planning on doing this all in python but I was denied my request as it was a "Security risk" to have python installed.

Any advise would be appreciated

2

There are 2 best solutions below

2
On

It's because the response is being cached. Either change the settings of IE so it fetches a fresh copy each time, or send some random parameter that is not used to trick IE into thinking it's a different "page". If you control the server, you can set the HTTP headers to force the client (if it conforms to standards) it always fetch the content. See more info here as well: VBScript: Disable caching of response from server to HTTP GET URL request

(I know most of this information is Sandeep's link. However, having it here is nicer and that might disappear.)

0
On

Maybe this will help you to reset the response:

Set s = CreateObject("MSXML2.XMLHTTP")
'Server authentication
s.open "GET", "fooAPI", False
s.send
do
    'Server data
    s.open "GET", "barAPI", False
    s.send
    Wscript.echo s.responseText
    WScript.Sleep 5000
    set s = nothing
loop