簡體   English   中英

緩存HTTP GET請求不適用於iOS 6

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

我已經編寫了問候詞應用程序來顯示我的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中:

#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

第一次發送請求時,一切正常,但是第二個調用使ios 6超時。iOS7正常工作。 有從服務器發送和接收的標頭:

第一個請求:

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

第一回應:

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

第二個請求:

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

第二回應:

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

這是來自控制台的日志:

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:

你知道發生了什么嗎?

第二個響應標頭中的不同之處在於狀態碼,即304 Not Modified 根據蘋果的說法:

通常,僅在滿足以下所有條件時才緩存響應

  • 該請求是針對HTTP或HTTPS URL(或您自己的支持緩存的自定義網絡協議)。

  • 請求成功(狀態代碼在200–299范圍內)。

  • 提供的響應來自服務器,而不是來自緩存。

  • 會話配置的緩存策略允許緩存。

  • 提供的NSURLRequest對象的緩存策略(如果適用)允許緩存。

  • 服務器響應(如果存在)中與緩存相關的標頭允許緩存。 響應大小足夠小,可以合理地放入緩存中。 (例如,如果您提供磁盤緩存,則響應必須不大於磁盤緩存大小的5%。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM