2014-11-25 53 views
0

从我所有读过的gzip压缩响应数据应该由NS URL加载系统自动解压缩。我正在使用AFNetworking和AFJSONResponseSerializer处理对自定义后端的请求。我已经尝试将"Accept-Encoding" = gzip添加到我的请求标题中,但它没有效果。我的响应头包括:自动解压缩json响应数据不起作用

"Content-Encoding" = gzip; 
"Content-Length" = 179837; 
"Content-Type" = "application/json"; 

我提出的要求是这样的:

[[AFHTTPRequestOperationManager sharedClient] 
    GET:@"some/endpoint" 
    parameters:@{@"foo":@"bar"} 
    success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"Response data size %u", operation.responseData.length); 
     NSURL *outputURL = [[APP_DELEGATE applicationDocumentsDirectory] URLByAppendingPathComponent:@"data.gzip"]; 
     [operation.responseData writeToURL:outputURL atomically:NO]; 
    } 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"ERROR [%d] [%@]", error.code, error.localizedDescription); 
    }]; 

当这个运行的operation.responseData.length打印尺寸为我所期望的。但是,responseObject应该是NSArray时为零。我添加了编写operation.responseData的行来测试收到的数据是我所期望的。我可以浏览到data.gzip,解压缩并查看我的JSON。

在我看来,在我的AFJSONResponseSerializer尝试处理它之前,收到的压缩数据没有被自动解压缩。我在Stackoverflow上找到的大部分内容都讨论了解压缩是自动还是建议添加一个"Accept-Encoding" = gzip标头。任何建议,将不胜感激。

回答

0

您是否检查过“自定义后端”的输出 - 使用类似Charles Proxy的内容,这样可以让您看到诸如响应标题和原始内容之类的内容。

例如如查尔斯所示的典型标题:

HTTP/1.1 200 OK 
Date Wed, 26 Nov 2014 22:25:33 GMT 
Server Apache/2.2.24 (Unix) mod_hive/4.0 mod_ssl/2.2.24 OpenSSL/1.0.0-fips mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_fcgid/2.3.6 
X-Powered-By PHP/5.3.29 
P3P CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM" 
Cache-Control no-cache 
Pragma no-cache 
Set-Cookie 4d3750fca133ae462ca26ca5a1cc0104=c7da991b30f53342846fff5ddaf3ea08; path=/ 
Cache-Control max-age=3600 
Expires Wed, 26 Nov 2014 23:25:33 GMT 
Host-Header 192fc2e7e50945beb8231a492d6a8024 
Keep-Alive timeout=5, max=100 
Connection Keep-Alive 
Transfer-Encoding chunked 
Content-Type text/html; charset=utf-8