简体   繁体   中英

Delphi XE5 iOS TidHTTP.get gets old version of file. Cached perhaps

Using Delphi XE5 on iOS.

I am trying to get a simple text file from a server using the following:

filename := DocumentDir + theFile.txt;
myFile := TFileStream.Create(filename, fmCreate);
aIDHTTP := TIdHTTP.Create(nil);    
aIdHTTP.Get('http://www.TheServer.dk/TheDir/theFile', myFile);

and populating a ListView later

sl := TStringList.Create;
sl.loadFromFile(filename, TEncoding.ANSI);
ListView1.clear;
for i := 0 to sl.count - 1 do
begin
  aItem := Listview1.add;
  aItem.text := sl[i];
end;

It actually works fine, but when I change the content of the file on the server, I get the old data in my listview when re-running the code.

Do I need somewhere to clear the cache, if so.. How do I do that?

Thanks in advance for any help. Kind Regards Jens Fudge

There's no caching built into TIdHTTP.GET .

If TIdHTTP were caching, it would send a conditional get (using an If-Modified-Since header), and if the server were cooperating, it would send back a HTTP 304 response from the server. If this were the case, normally, your web server would be able to detect that the file changed and would not return a 304 , but instead serve up the most recent version of the file. If this were the issue, and it's most likely not, it would point to an issue that you'd resolve on the server.

If you are experiencing a caching issue between the client and server using TIdHTTP.GET , it's possible that the client is going through a caching web proxy (and you may not even be aware of it) before it reaches the web server. Often, ISPs implement caching web proxies to reduce their bandwidth costs.

To signal caching mechanisms not to respond with a cached response, you can try including the Cache-Control: no-cache header, or for backwards compatibility with HTTP/1.0, you could specify the Pragma: no-cache header. If neither of those work for you, you may have to get more sophisticated.

As whsrdaddy suggested, one way to defeat an overzealous web caching proxy is to send an extra parameter that changes on each request, such as the date and time (down to the millisecond), or a randomly generated value. This way, the caching web proxy will forward the request to the web server rather than serving up a cached result simply because the URL changed.

POST requests don't have the same problem, so be sure to use POST whenever sending data to the server (modifications), and only use GET when requesting resources (read only).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM