ios - 无法使用 AFNetworking 下载 .zip 文件

标签 ios objective-c afnetworking

我可以使用 font squirrel API 的所有其他端点,但是每当我尝试获取返回压缩字体套件的端点时,我都会收到 Request failed: unacceptable content-type: "application/x -zip” 和标题看起来像

status code: 200, headers {
Connection = \"keep-alive\";
\"Content-Disposition\" = \"attachment; filename=\\\"1942-report-fontfacekit.zip\\\"\";
\"Content-Length\" = 284695;
\"Content-Transfer-Encoding\" = binary;
\"Content-Type\" = \"\\\"application/x-zip\\\"\";
Date = \"Fri, 11 Apr 2014 23:21:37 GMT\";
Expires = 0;\n    Pragma = \"no-cache\";
Server = \"nginx admin\";
\"Set-Cookie\" = \"admin_rm=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; domain=www.fontsquirrel.com, sitetoken=9e7e8c6f53ffe68791fa9e6a2531fe88; expires=Mon, 08-Apr-2024 23:21:37 GMT; path=/; domain=www.fontsquirrel.com, tracker=b13703d93358d65f09306b1b09d6e072; path=/; domain=www.fontsquirrel.com\";
\"X-Cache\" = \"HIT from Backend\";
\"X-Powered-By\" = \"PHP/5.3.23\";
\"X-UA-Compatible\" = \"IE=Edge,chrome=1\";

这对我来说真的很奇怪,因为我尝试将响应序列化器设置为 XML、JSON 和 HTTP,每个序列化器都带有 acceptableContentTypes = @"text/html",@"application/x-zip" 添加。我不确定我是否做错了什么,或者我是否应该要求 API 开发人员澄清他们是否有问题。我可以从 chrome 和curl 发出完全相同的 GET 请求,并且它们都可以工作。

--编辑,添加代码片段 //初始化

+(instancetype)sharedManager {
static APIManager *manager;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
    manager = [[APIManager alloc] initWithBaseURL:[NSURL URLWithString:@"http://www.fontsquirrel.com/"]];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/x-zip",nil];
});
return manager;

}

//获取

-(AFHTTPRequestOperation*)downloadFontFamily:(FontFamily*)fontFamily withBlock:(void(^)(UIFont *UIFont, NSError *error))callback {
return [self GET:FORMAT(@"fontfacekit/%@",fontFamily.familyURLName) parameters:nil success:^(AFHTTPRequestOperation *operation,
                                                                                             id responseObject) {
    NSLog(@"%@%@",[responseObject class],responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"FAIL %@\n%@",error.localizedDescription,error.userInfo);
}];

}

最佳答案

返回的内容类型不是application/x-zip,而是“application-x-zip”,而且我还必须使用 HTTPResponseSerializer。

关于ios - 无法使用 AFNetworking 下载 .zip 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23024220/

相关文章:

iphone - 给定 iPhone UDID 的私有(private) iPhone 应用程序分发?

objective-c - AFNetworking:带有 application/x-www-form-urlencoded 的 POST 请求

ios - 将 UILabel 添加到 UITextField 的左侧

ios - 代码=-1011 "Request failed: unauthorized (401)"AFNetworking

ios - 如何在 AFNetworking 中设置授权 header

ios - 在长按手势时在 UIButton 中显示图像

ios - 使用 OpenCV 在卡片边缘绘制轮廓

ios - 如何更改 iOS 的小数位数?

iphone - NSDateFormatter 无法正常工作?

objective-c - 用于 Objective-C 的 CoffeeScript 类语言?