首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >HTTP GET请求的缓存不适用于iOS 6

HTTP GET请求的缓存不适用于iOS 6
EN

Stack Overflow用户
提问于 2014-02-28 22:38:41
回答 1查看 226关注 0票数 1

我已经编写了hello word应用程序来显示我的HTTP GET请求缓存问题。

代码语言:javascript
运行
复制
#import "StartViewController.h"

@interface StartViewController () <NSURLConnectionDataDelegate>

@property (nonatomic, strong) NSURLConnection *connection; @end

@implementation StartViewController

- (IBAction)buttonAction:(id)sender {
    NSLog(@"buttonAction");
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://mycoolpage.com/phones"] ];
    self.connection = [[NSURLConnection alloc] initWithRequest:request delegate:self ]; }

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"didReceiveResponse"); }

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"didReceiveData:"); }


- (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse {
    NSLog(@"willCacheResponse");
    return cachedResponse; }

@end

AppDelegate:

代码语言:javascript
运行
复制
#import "AppDelegate.h"
#import "StartViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.rootViewController = [[StartViewController alloc] init];

    [self.window makeKeyAndVisible];

    NSURLCache *defaultCache = [NSURLCache sharedURLCache];
    NSURLCache *applicationCache = [[NSURLCache alloc] initWithMemoryCapacity:defaultCache.memoryCapacity diskCapacity:128*1024*1024 diskPath:@"MobileCache"];
    [NSURLCache setSharedURLCache:applicationCache];

    return YES;
}

@end

当第一次发送请求时,一切正常,但第二次调用在iOS6上超时。ios 7工作没有任何问题。有从服务器发送和接收的标头:

第一个请求:

代码语言:javascript
运行
复制
GET /phones HTTP/1.1
Host    mycoolpage.com
Accept-Encoding gzip, deflate
Accept  */*
Accept-Language en-us
Connection  keep-alive
User-Agent  CacheDemo/1.0 CFNetwork/609 Darwin/13.1.0

第一个响应:

代码语言:javascript
运行
复制
HTTP/1.1 200 OK
Cache-Control   private, max-age=0
Content-Encoding    gzip
Content-Type    application/json; charset=UTF-8
Date    Fri, 28 Feb 2014 14:28:24 GMT
ETag    afcf39d75c69c694f4dfaca7f20b816b
Content-Length  28578
Connection  keep-alive

第二个请求:

代码语言:javascript
运行
复制
GET /phones HTTP/1.1
Host    mycoolpage.com
If-None-Match   afcf39d75c69c694f4dfaca7f20b816b
Accept-Encoding gzip, deflate
Accept  */*
Accept-Language en-us
Connection  keep-alive
User-Agent  CacheDemo/1.0 CFNetwork/609 Darwin/13.1.0

第二个响应:

代码语言:javascript
运行
复制
HTTP/1.1 304 Not Modified
Cache-Control   private, max-age=0
Content-Encoding    gzip
Content-length  22
Content-Type    text/plain; charset=UTF-8
Date    Fri, 28 Feb 2014 14:29:15 GMT
ETag    afcf39d75c69c694f4dfaca7f20b816b
Connection  keep-alive

以下是来自控制台的日志:

代码语言:javascript
运行
复制
2014-03-02 16:58:29.054 CacheDemo[6834:907] buttonAction
2014-03-02 16:58:29.286 CacheDemo[6834:907] response:<NSHTTPURLResponse: 0x784a2f0>
2014-03-02 16:58:29.286 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.287 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.351 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.352 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.352 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.353 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.353 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.354 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.355 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.356 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.356 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.356 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.415 CacheDemo[6834:907] didReceiveData:
2014-03-02 16:58:29.416 CacheDemo[6834:907] willCacheResponse
2014-03-02 16:58:35.095 CacheDemo[6834:907] buttonAction
// Timeout after 1 min
2014-03-02 16:59:34.754 CacheDemo[6834:907] response:<NSHTTPURLResponse: 0x766a710>
2014-03-02 16:59:34.754 CacheDemo[6834:907] didReceiveData:

你知道这是怎么回事吗?

EN

回答 1

Stack Overflow用户

发布于 2015-04-15 16:33:56

第二个响应头中的一个不同之处是状态码,它是304 Not Modified。根据Apple的说法

通常,只有在满足以下所有条件时,才会缓存响应

  • 请求HTTP或HTTPS URL (或您自己支持缓存的自定义网络协议)。
  • 请求成功(状态代码在200-299范围内)。
  • 提供的响应来自服务器,而不是来自缓存。
  • 会话配置的缓存策略允许缓存。

< NSURLRequest >H113提供的NSURLRequest对象的缓存策略(如果适用)允许缓存。

  • 服务器响应中与缓存相关的标头(如果存在)允许缓存。响应大小足够小,可以合理地放入缓存中。(例如,如果提供磁盘缓存,则响应大小不得大于磁盘缓存大小的5%。)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22098124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档