当“Content-Encoding:gzip”时,AFNetworking无法恢复下载。已设定 [英] AFNetworking fails to resume downloads when "Content-Encoding: gzip" is set

查看:196
本文介绍了当“Content-Encoding:gzip”时,AFNetworking无法恢复下载。已设定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AFNetworking2从我的AWS S3存储桶下载文件
除非我为文件设置Content-Encoding:gzip,否则一切正常。
AFNetworking无法下载从服务器
返回的部分内容响应,它给我以下错误:

I'm using AFNetworking2 to download files from my AWS S3 bucket Everything works fine except when I set the Content-Encoding: gzip for the files. AFNetworking fails to download the partial content response coming back from the server and it gives me the following error:


错误域= NSURLErrorDomain代码= -1015无法解码原始数据
UserInfo = 0x10d2d8ce0 {NSUnderlyingError = 0x10d1ace80无法解码原始
数据,
NSErrorFailingURLStringKey = http://s3.amazonaws.com/awdtest/fullzip.pdf
NSErrorFailingURLKey = http://s3.amazonaws.com/awdtest/fullzip.pdf
NSLocalizedDescription =不能解码原始数据}

Error Domain=NSURLErrorDomain Code=-1015 "cannot decode raw data" UserInfo=0x10d2d8ce0 {NSUnderlyingError=0x10d1ace80 "cannot decode raw data", NSErrorFailingURLStringKey=http://s3.amazonaws.com/awdtest/fullzip.pdf, NSErrorFailingURLKey=http://s3.amazonaws.com/awdtest/fullzip.pdf, NSLocalizedDescription=cannot decode raw data}

然而,当我从我的文件中删除Content-Encoding:gzip元数据时,它工作正常。
我知道我的服务器支持范围请求,我已经使用其他方法对其进行了测试,并且工作正常。

however, when I remove the "Content-Encoding: gzip" metadata from my file, it works fine. I know that my server supports range request and I have tested it using other methods and it worked fine.

这里是我服务器的HTTP响应:

here is the HTTP response from my server:


HTTP / 1.1 200 OK

HTTP/1.1 200 OK

x-amz-id-2:
k5b65TtAgrD5Cn3N2aixCCdi6qAmg4j9iuOSNaO0uMRKLHPTQ + DMaA20u9j1CNzA

x-amz-id-2: k5b65TtAgrD5Cn3N2aixCCdi6qAmg4j9iuOSNaO0uMRKLHPTQ+DMaA20u9j1CNzA

x-amz-request-id:7AE5A7DD81ED2B88

x-amz-request-id: 7AE5A7DD81ED2B88

日期:星期五,星期五,16 2014年5月04:45:17 GMT

Date: Fri, 16 May 2014 04:45:17 GMT

Content-Encoding:gzip

Content-Encoding: gzip

Last-Modified:Fri,16 2014年5月04:44:51 GMT

Last-Modified: Fri, 16 May 2014 04:44:51 GMT

ETag:88bbe0b318bf11dd56a31176d3384e78

ETag: "88bbe0b318bf11dd56a31176d3384e78"

Accept-Ranges:bytes

Accept-Ranges: bytes

内容类型:application / pdf

Content-Type: application/pdf

内容长度:1243325

Content-Length: 1243325

服务器:AmazonS3

Server: AmazonS3

以下是我正在使用的示例文件:

Here are the sample files that I'm using:

https://s3.amazonaws.com/awdtest/ full.pdf

https ://s3.amazonaws.com/awdtest/fullzip.pdf (gzip并将Content-Encoding设置为gip)

https://s3.amazonaws.com/awdtest/fullzip.pdf (gzipped and have the Content-Encoding set to gip)

如果我愿意的话有人可以帮我解决这个问题。

I would appreciate it if someone could help me with this issue.

PS:我已经尝试过以下但仍然无效

PS: I have already tried the followings and it still does not work

[request setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"];


[request setValue:@"deflate" forHTTPHeaderField:@"Accept-Encoding"];


[request setValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];


推荐答案

好的,不确定你的下载方式这个,或者你的目的是什么。所以,我假设您正在使用iOS 7 API,特别是AFHTTPSessionManager,并且您正在使用该文件的原始数据。鉴于此,我编写了一个数据序列化器来使用,然后实际代码很短:

Ok, not sure how you're going about downloading this, or what your purpose is. So, I'm assuming you're using the iOS 7 APIs, specifically an AFHTTPSessionManager, and you're after the raw data of the file. Given that, I wrote a Data serializer to use, and then the actual code is very short:

Serializer(有点奇怪):

Serializer (kinda weird):

@interface FFDataResponseSerializer : AFHTTPResponseSerializer

@end

@implementation FFDataResponseSerializer

- (instancetype)init
{
    self = [super init];
    if (!self)
    {
        return nil;
    }

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/pdf", nil];

    return self;
}

- (id)responseObjectForResponse:(NSURLResponse *)response
                           data:(NSData *)data
                          error:(NSError *__autoreleasing *)error
{
    if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error])
    {
        return nil;
    }

    return data;
}

@end

然后,在我的代码中:

NSURLSessionConfiguration *aURLSessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
aURLSessionConfiguration.HTTPAdditionalHeaders = @{@"Accept-Encoding": @"gzip, deflate",
                                                   @"Accept": @"application/pdf"};
AFHTTPSessionManager *urlManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:aURLSessionConfiguration];

urlManager.responseSerializer = [FFDataResponseSerializer serializer];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://s3.amazonaws.com/awdtest/fullzip.pdf"]];

NSURLSessionDataTask *dataTask = [urlManager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
    if (error)
    {
        NSLog(@"error: %@", error);
    }
    else
    {
     NSLog(@"response: %@, responseObject: %@", response, responseObject);
     // responseObject is your NSData Object with the PDF data inside of it.
    }
}];

[dataTask resume];

你显然想要处理错误,保留你想要重用的变量,等等...

You'll obviously want to handle errors, keep ahold of the variables you'll want to reuse, etc, etc...

这篇关于当“Content-Encoding:gzip”时,AFNetworking无法恢复下载。已设定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆