2013-10-10 103 views
4

我正在使用一個簡單的AFNetworking [AFHTTPRequestOperation -initWithRequest:]下載文件,我想檢查響應標題「內容長度」,然後才能真正下載文件。 如何檢查AFNetworking中的響應內容長度?AFNetworking - 獲取響應內容長度

+0

afnetworking是開源項目,所以我認爲你可以更新 - (無效)連接:(NSURLConnection的__unused *)連接 didReceiveResponse:(NSURLResponse *)AFURLConnectionOperation.m的反應方法,它只是使用簡單NSURLConnection的,您可以添加屬性塊將在didReceiveResponse方法中調用,並取消請求:@property(nonatomic,copy)YourBlockType didReciveLengthCallback; – BergP

回答

7

使用expectedContentLengthNSURLResponse的屬性你可以找到長度。

AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 
    [op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) 
    { 
     NSLog(@"Content-lent: %lld", [operation.response expectedContentLength]); 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"Error: %@", error); 
    }];