4

当我尝试使用 AFNetworking 从 URL 下载 pdf 文件时,保存的文件大小大约是我预期的一半。当我使用 curl/标准浏览器工具发出相同的请求时,文件大小是正确的。

如何即时解压缩文件以使其可读?或者 AFNetworking 是否会自动压缩它不应该压缩的文件?

AFNetworking 的客户端类默认设置以下请求标头:

"Accept-Encoding" = gzip;
"Accept" = application/json";

这是我获取文件的方式:

AFHTTPClient *client; // already instantiated & configured
NSURLRequest *request = [client requestWithMethod:@"GET" path:requestPath parameters:parameters];
AFHTTPRequestOperation *operation = [client HTTPRequestOperationWithRequest:request success:success failure:failure];

operation.outputStream = [NSOutputStream outputStreamToFileAtPath:localFilePath append:NO];

[self enqueueHTTPRequestOperation:operation];

后来,当我通过 NSFileManager 和外部工具检查文件大小时,大小是我期望的 1/2。

当我通过 HTTP 浏览器客户端发出相同的请求时,我在响应标头中看不到任何表明返回的内容已压缩的内容。

服务器发送的响应头(通过 HTTP 浏览器扩展工具检查):

Transfer-Encoding: chunked
Status: 200 OK
Content-Transfer-Encoding: binary
Content-Disposition: inline; filename="document_sample.pdf"
Connection: keep-alive
Content-Type: application/pdf
Cache-Control: private

如何下载文件以便按预期保存?

4

0 回答 0