2013-08-06 84 views
4

我正在使用etag緩存我的圖像,它第一次下載所有圖像時應該如此,但是第二次它應該進入失敗的塊時與304AFNetworking在應該接收304時返回200 - ETag

我試着提出請求外,我得到304像我應該,它只是與我有麻煩

NSString *urlString = [API_BASE_URL_PHOTOS stringByAppendingPathComponent:[photo getPathToPhoto]]; 
    NSString *properlyEscapedURL = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
    NSURL *url = [NSURL URLWithString:properlyEscapedURL]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; 
    if ([UserDefaultManager getEtagForKey:photo.nom] != nil) { 
     [request setValue:[UserDefaultManager getEtagForKey:photo.nom] forHTTPHeaderField:ETAG_IF_NONE_MATCH]; 
    } 
    [request setHTTPMethod:API_METHOD_GET]; 

AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request imageProcessingBlock:nil 
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image) { 

    [FileUtils createDirectoryInDocumentsWithFileName:photo.folderName]; 
    NSString *docPath = [FileUtils getPathInDocumentsWithFileName:[photo getPathToPhoto]]; 

    // Save Image 
    NSData *imageData = UIImageJPEGRepresentation(image, 90); 
    [imageData writeToFile:docPath atomically:YES]; 


    if ([response respondsToSelector:@selector(allHeaderFields)]) { 
     NSDictionary *allHeaderFields = [response allHeaderFields]; 
     [UserDefaultManager setEtag:[allHeaderFields objectForKey:ETAG] forKey:photo.nom]; 
    } 


    if ([[DownloadManager sharedManager].downloadQueue.operations count] == 0) { 
     [[NSNotificationCenter defaultCenter] postNotificationName:NOTIFICATION_DOWNLOAD_ALL_PHOTOS_FINISHED object:nil]; 
    } 
} 
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) { 
    if (response.statusCode != RESPONSE_CODE_PHOTO_UP_TO_DATE) { 
     LogDebug(@"%@", [error localizedDescription]); 
    } 

}]; 
+1

如果您在外部提出請求並獲得304,並且您在AFNetworking中創建並獲取200,那麼您可能會以不同方式提出請求。 AFNetworking不包含任何將更改服務器返回的HTTP狀態代碼的代碼。 –

+0

是的,我設法算起來,我改變請求到 NSMutableURLRequest *請求= [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] 的CachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; 並且做了竅門 –

回答

6

AFNetworking我設法改變我的要求來解決這個問題

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] 
                   cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
                  timeoutInterval:60]; 

希望這可以幫助

+0

NSURLRequest的默認超時間隔爲60秒;我會堅持,除非你有一個原因,你切換到10. –

+0

謝謝,我把它改爲60,它被設置爲10在我看到的例子中 –

相關問題