none
how to disable caching HTTP GET in metro app, I am using IXMLHTTPRequest2

    Question

  • I am doing an http GET to fetch data, I am using IXMLHTTPRequest2.

    If I GET url "http://foo.com" (curl "http://foo.com"), the second time I get this url again, the content on server is actually changed, but what I am getting is the cached result.

    It seems caching only honor URL, so if different header with same URL, still same cached result.

    I have tried "Cache-Control: no-cache", "Cache Control: no-store", and "Pragma: no-cache". None of them are honored by the API.

    Is there a way to turn cache off or walk around? (One walk around I am using is appending garbage at end of URL but I am not feeling good with it).

    Thanks,

    Helin


    • Edited by Helin Wang Monday, October 29, 2012 10:47 PM
    Monday, October 29, 2012 10:40 PM

Answers

  • Hello Helin Wang, Henador,

    You can force XHR to retrieve the latest content by setting the "If-Modified-Since" HTTP header in the request and set a time in the past.

    If you have control over the server response, you could send back an Expires HTTP response header with a value 0 or a date in the past. That should make XHR retrieve the latest response for you.

    You are only required to do one of the above, changing both the client and server side code is not necessary.

    The client side code could be changed to something like this:

    xhr->Open(...)

    xhr->SetRequestHeader(L"If-Modified-Since", L"Sat, 01 Jan 2000 00:00:01 GMT");

    xhr->Send(...)

    For changing the server side behavior if your server side code is based on ASP.net you could change your response header like this:

    Response.Headers.Add("Expires", "0")

    Thanks,

    Prashant.

    Tuesday, October 30, 2012 12:05 AM

All replies

  • Looks suspiciously like my post a couple of days ago:

    http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/dadbea2e-85d4-4af0-9283-2f91e5b7cc2c

    There is no solution on the client side. The cache is always used, which is ridiculous.

    Those cache headers don't mean anything to XHR2 when used in the GET request. I have verified that if the *server* inserts those cache control statements in its response, XHR2 does not cache the result locally.

    So, we have to keep putting junk parameters at the ends of our URLs (e.g. "?blah={time}") to defeat this built-in caching. Makes our apps look stupid to the server admin when he looks at his access logs...

    Monday, October 29, 2012 11:43 PM
  • Hello Helin Wang, Henador,

    You can force XHR to retrieve the latest content by setting the "If-Modified-Since" HTTP header in the request and set a time in the past.

    If you have control over the server response, you could send back an Expires HTTP response header with a value 0 or a date in the past. That should make XHR retrieve the latest response for you.

    You are only required to do one of the above, changing both the client and server side code is not necessary.

    The client side code could be changed to something like this:

    xhr->Open(...)

    xhr->SetRequestHeader(L"If-Modified-Since", L"Sat, 01 Jan 2000 00:00:01 GMT");

    xhr->Send(...)

    For changing the server side behavior if your server side code is based on ASP.net you could change your response header like this:

    Response.Headers.Add("Expires", "0")

    Thanks,

    Prashant.

    Tuesday, October 30, 2012 12:05 AM