简体   繁体   English

Delphi XE5 iOS TidHTTP.get获取旧版本的文件。 可能已缓存

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

Using Delphi XE5 on iOS. 在iOS上使用Delphi XE5。

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 并稍后填充ListView

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 . 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. 如果TIdHTTP正在缓存,它将发送条件获取(使用If-Modified-Since标头),并且如果服务器正在协作,则它将从服务器发送回HTTP 304响应。 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. 如果是这种情况,通常,您的Web服务器将能够检测到文件已更改,并且不会返回304 ,而是提供文件的最新版本。 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. 如果使用TIdHTTP.GET在客户端和服务器之间遇到缓存问题,则可能是客户端在到达Web服务器之前正在通过缓存Web代理(您甚至可能没有意识到)。 Often, ISPs implement caching web proxies to reduce their bandwidth costs. ISP通常会实现缓存Web代理以降低其带宽成本。

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. 要用信号通知缓存机制不响应缓存的响应,可以尝试包括Cache-Control: no-cache标头,或者为了与HTTP / 1.0向后兼容,可以指定Pragma: no-cache标头。 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. 正如whsrdaddy所建议的,一种克服过度热情的Web缓存代理的方法是发送一个随每个请求而变化的额外参数,例如日期和时间(低至毫秒)或随机生成的值。 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. 这样,缓存Web代理会将请求转发到Web服务器,而不是仅仅因为URL更改就提供了缓存结果。

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). POST请求不会有相同的问题,因此请确保在将数据发送到服务器时(修改)使用POST ,并且仅在请求资源时使用GET (只读)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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