views:

249

answers:

2

I want to turn off the cache used when a URL call to a server is made from VBScript running within an application on a Windows machine. What function/method/object do I use to do this?

When the call is made for the first time, my Linux based Apache server returns a response back from the CGI Perl script that it is running. However, subsequent runs of the script seem to be using the same response as for the first time, so the data is being cached somewhere. My server logs confirm that the server is not being called in those subsequent times, only in the first time.

This is what I am doing. I am using the following code from within a commercial application (don't wish to mention this application, probably not relevant to my problem):


With CreateObject("MSXML2.XMLHTTP")
  .open "GET", "http://myserver/cgi-bin/nsr/nsr.cgi?aparam=1", False
  .send
  nsrresponse =.responseText
End With

Is there a function/method on the above object to turn off caching, or should I be calling a method/function to turn off the caching on a response object before making the URL?

I looked here for a solution: http://msdn.microsoft.com/en-us/library/ms535874(VS.85).aspx - not quite helpful enough. And here: http://www.w3.org/TR/XMLHttpRequest/ - very unfriendly and hard to read.

I am also trying to force not using the cache using http header settings and html document header meta data:

Snippet of server-side Perl CGI script that returns the response back to the calling client, set expiry to 0.


    print $httpGetCGIRequest->header(
        -type    => 'text/html',
        -expires => '+0s',
        );

Http header settings in response sent back to client:


<html><head><meta http-equiv="CACHE-CONTROL" content="NO-CACHE"></head>
<body>
response message generated from server
</body>
</html>

The above http header and html document head settings haven't worked, hence my question.

+1  A: 

I don't think that the XMLHTTP object itself does even implement caching.

You send a fresh request as soon as you call .send() on it. The whole point of caching is to avoid sending requests, but that does not happen here (as far as your code sample goes).

But if the object is used in a browser of some sort, then the browser may implement caching. In this case the common approach is to include a cache-breaker into the statement: a random URL parameter you change every time you make a new request (like, appending the current time to the URL).

Alternatively, you can make your server send a Cache-Control: no-cache, no-store HTTP-header and see if that helps.

The <meta http-equiv="CACHE-CONTROL" content="NO-CACHE> is probably useless and you can drop it entirely.

Tomalak
Thank you very much for your time Tomalak. I found that WinHttpRequest.5.1 COM object does not cache. I will have to do wider testing to gain confidence, however. +1 for the different-URL-each-time tip - that is a definitely a tip to consider - I may use that as well as WinHttpRequest.5.1 and extend our server to cope with that extra URL parameter. I will probably include a time-of-request field to high precision, e.g. millisecs perhaps. It's definitely worth considering as part of the solution.
Rob
now the accepted answer as your random url suggestion hits the route cause - caching and breaks this so that the code is forced to contact the server each time.
Rob
To make a unique session ID number, I'm using the Date and Timer built in VB functions, a random number, along with the computer name/hostname of the PC where the script Date gives todays date, Timer gives the time elapsed in milliseconds. Along with the random number and machine id, this should make the HTTP GET URL call unique. The chances of 2 identical URL calls being the same are zero.
Rob
Rob
Rob
@Rob: Did you also try the `Cache-Control: no-cache` HTTP header? This should prevent caching as well. (But the cache-breaker will definitely work, no matter the circumstances.)
Tomalak
+1  A: 

You could use WinHTTP, which does not cache HTTP responses. You should still add the cache control directive (Cache-control: no-cache) using the SetRequestHeader method, because it instructs intermediate proxies and servers not to return a previously cached response.

Garett
So the XMLHTTP object *does* cache requests?
Tomalak
Actually, no it should not, which supports your original point.
Garett
+1 and answered. I looked up WinHttp and used WinHttp.WinHttpRequest.5.1. after a little extra research. Your suggestion provided the seed to this, credit to you. This is successful in that the caching does not occur. What I need to do now is widen my testing (e.g. on different machines/configurations) of this script to increase confidence. Is there any reason why the default caching configuration of this object would vary between machines?
Rob
I’ve used it in an application that has been deployed across several versions of Windows without change. So, from my experience its behavior has been consistent. As a side note, I now recall that for this application I also looked at ServerXMLHTTP, which is based on WinHTTP. Unlike XMLHTTP, which is based on WININET, and may explain why you were experiencing caching. See the following for more details: http://msdn.microsoft.com/en-us/library/ms762278(VS.85).aspx
Garett
This answer to use WinHttp... solved my caching problems on my machine and I will use it. But I still have uncertainty as to whether I can guarantee this same non-caching behaviour on all machines. I've moved the accepted answer to Tomalak as they mention the random URL parameter as a cache-breaker. This squarely hits and solves my problem at the route cause. I've now tried this out and the caching is avoided (As required) if I use either MSXML2.XMLHTTP or WinHttpRequest.5.1. Sorry about switching the accepted answer - in compensation I've +1'd both your comments here - they are useful.
Rob
No problem, glad you you were able to find a resolution.
Garett