ASIHTTPRequest似乎总是缓存JSON数据 [英] ASIHTTPRequest seems to cache JSON data always

查看:99
本文介绍了ASIHTTPRequest似乎总是缓存JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASIHTTPRequest API从Web端获取一些JSON数据.我使用的是异步请求,而没有更改默认缓存属性.代码如下:

I'm using ASIHTTPRequest API to get some JSON data from a web side. I'm using an asynchronous request without changing default cache properties. Code is as follows:

__unsafe_unretained ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL:url];

request.timeOutSeconds = 30;
[request setCompletionBlock:^(void)
 {
     NSString *str = [request responseString];
     /*
     *
     *
     */
 }];
[request setFailedBlock:^(void)
 {
     NSLog(@"status: %@",[request responseStatusMessage]);
     NSLog(@"%@ : %@",[request url],[[request error] debugDescription]);         
 }];
[request startAsynchronous];

但是,尽管服务器中JSON数据的内容发生了变化,但获得的JSON数据仍然是旧数据.

However, obtained JSON data remains as the old one although content of JSON data in server changes.

我在笔记本电脑和iPhone Safari上都使用网络浏览器检查了数据.当我在JSON更改后请求url时,它首先返回旧数据,但是如果刷新页面,则它返回更新的数据.但是在应用程序中,ASIHTTPRequest始终返回旧数据.

I checked data using web browser, both on laptop and on iPhone safari. When i request url after a change in JSON, it first returns old data, but if i refresh the page, it returns updated data. But in the app, ASIHTTPRequest always returns the old data.

我还尝试调试ASIHTTPRequest代码,以查看是否使用了任何缓存的数据.但是似乎它从未使用过下载缓存,因为它尚未设置.它永远不会输入[useDataFromCache]方法.

I also try to debug ASIHTTPRequest code in order to see whether any cached data used. But it seems like it never uses download cache because it has not been set. It never enters [useDataFromCache] method.

可能是什么问题?如何强制ASIHTTPRequest检查服务器上是否有更新的数据,并使其获取真实的更新的JSON?

What could be the problem? How can i force ASIHTTPRequest to check whether there is an updated data on server, and make it get the true updated JSON?

编辑

我使用了Cache-Control标头,现在我得到了正确的更新的JSON数据.代码如下:

I used Cache-Control header, and now i get the correct updated JSON data. Code is as follows:

[request addRequestHeader:@"Cache-Control" value:@"no-cache"];

但是,我认为从现在开始请求,即使未修改JSON也会始终尝试检索JSON,这会降低性能.

However, i think from now on request will always try to retrieve JSON even if it is not modified, which will decrease performance.

我如何使其首先检查服务器是否修改了数据,然后检索是否修改了数据?目前,我从php网址获取JSON数据作为动态响应,因此没有文件可以检查数据的最新性.

How can i make it first check the server whether data is modified, and retrieve if it is modified? Currently i get JSON data as a dynamic response from a php url, so there is no file which i can check up to dateness of the data.

有什么解决方案?

此致

推荐答案

鉴于您所说的一切,ASIHTTPRequest似乎不太可能缓存响应.

Given everything you've said, it seems unlikely that ASIHTTPRequest is cacheing the response.

因此,还必须做些其他事情-似乎您和服务器之间有一个缓存代理服务器,这就是为什么设置Cache-Control会有所作为的原因.

So, something else must be - it seems like you have a cacheing proxy server inbetween you and the server, and that's why setting Cache-Control makes a difference.

它可能是您本地网络上的代理服务器,或者可能位于您的ISP上,或者可能位于您正在使用的Web服务器的前面.

It could be a proxy server on your local network, or it could be at your ISP, or it could be in front of the web server you're using.

这篇关于ASIHTTPRequest似乎总是缓存JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆