问题描述
我需要使用HTTP协议下载XML文件,以便在我的iPhone应用程序本地使用它。有时,此文件将在服务器上更新,但不经常更新。
I need to download an XML file, using HTTP protocol, to use it locally on my iPhone app. Occasionally this file will be updated on the server, but not very often.
如何将已下载的Documents文件夹中的文件与服务器上的文件进行比较,如果内容已在服务器上更新,则只需将其下载?
How do I compare the already downloaded file that is already in the Documents folder, with the one on the server, just downloading it if the content has been updated on the server?
推荐答案
首次下载文件时,请保存响应的Last-Modified标头中的日期。您可以将其从 NSHTTPURLResponse
对象中拉出来。
The first time you download the file, save the date from the Last-Modified header of the response. You can pull it out of the NSHTTPURLResponse
object.
在后续运行中,将该日期放在<$ c中$ c> If-Modified-Since NSURLRequest
的标题。如果服务器上的文件没有更改, NSHTTPURLResponse
的 statusCode
应为304(表示不修改后的)和响应的主体应为空。
On subsequent runs, put that date in the If-Modified-Since
header of the NSURLRequest
. If the file on the server hasn't changed, the statusCode
of the NSHTTPURLResponse
should be 304 (which means "Not Modified") and the body of the response should be empty.
这篇关于如果文件没有更改,请避免再次下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!