简体   繁体   English

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

[英]Caching for HTTP GET request doesn't work for iOS 6

I have written hello word application to show my problem with caching of HTTP GET request. 我已经编写了问候词应用程序来显示我的HTTP GET请求缓存问题。

#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: AppDelegate中:

#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

When request is send first time everything works great but the second call makes timeout on ios 6. iOS 7 works without any problems. 第一次发送请求时,一切正常,但是第二个调用使ios 6超时。iOS7正常工作。 There are headers sent and received from server: 有从服务器发送和接收的标头:

First Request: 第一个请求:

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

First Response: 第一回应:

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

Second Request: 第二个请求:

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

Second Response: 第二回应:

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

Here are logs from console: 这是来自控制台的日志:

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:

Do you know what is going on ? 你知道发生了什么吗?

One thing that is different in the second response header is the status code, it is 304 Not Modified . 第二个响应标头中的不同之处在于状态码,即304 Not Modified According to Apple : 根据苹果的说法:

As a rule, responses are cached only when all of the following are true 通常,仅在满足以下所有条件时才缓存响应

  • The request is for an HTTP or HTTPS URL (or your own custom networking protocol that supports caching). 该请求是针对HTTP或HTTPS URL(或您自己的支持缓存的自定义网络协议)。

  • The request was successful (with a status code in the 200–299 range). 请求成功(状态代码在200–299范围内)。

  • The provided response came from the server, rather than out of the cache. 提供的响应来自服务器,而不是来自缓存。

  • The session configuration's cache policy allows caching. 会话配置的缓存策略允许缓存。

  • The provided NSURLRequest object's cache policy (if applicable) allows caching. 提供的NSURLRequest对象的缓存策略(如果适用)允许缓存。

  • The cache-related headers in the server's response (if present) allow caching. 服务器响应(如果存在)中与缓存相关的标头允许缓存。 The response size is small enough to reasonably fit within the cache. 响应大小足够小,可以合理地放入缓存中。 (For example, if you provide a disk cache, the response must be no larger than about 5% of the disk cache size.) (例如,如果您提供磁盘缓存,则响应必须不大于磁盘缓存大小的5%。)

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

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