如何禁用都市应用程序中的缓存HTTP GET,我正在使用IXMLHTTPRequest2

问题描述:

我正在执行http GET来获取数据,我正在使用IXMLHTTPRequest2.

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

如果我获取网址"http://foo.com"(卷曲"http://foo.com"),则第二次再次获取该网址时,服务器上的内容实际上已更改,但是我是得到就是缓存的结果.

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.

缓存似乎仅是荣誉URL,因此,如果不同的标头具有相同的URL,则缓存的结果仍相同. 我已经尝试过"Cache-Control:不缓存","Cache Control:不存储"和"Pragma:no-cache". API都不尊重它们.

Caching seems 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.

有没有办法关闭缓存或四处走走? (我正在使用的一种方法是在URL的末尾附加垃圾,但我对此感觉不太好.)

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).

我的问题在这里由Prashant回答:

my question got answered here by Prashant: http://social.msdn.microsoft.com/Forums/en-US/winappswithnativecode/thread/1df95d3e-68c9-4351-822a-c2cfde380248/#1df95d3e-68c9-4351-822a-c2cfde380248

您可以通过在请求中设置"If-Modified-Since" HTTP标头并设置过去的时间来强制XHR检索最新内容. 如果您可以控制服务器响应,则可以发送回Expires HTTP响应标头,其值为0或过去的日期.这应该使XHR为您检索最新的响应.

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(...)

要更改服务器端行为,如果您的服务器端代码基于ASP.net,则可以这样更改响应头:

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")