如果调用下载功能,请设置" 授权"在Http Header中,并调用第二个函数" downloadTaskWithRequest ",但http标头还包括" 授权"选项,从代码中,我没有设置选项,如何删除" 授权"调用第二个函数之前的选项,谢谢!
- (void)download:(NSString *)api source:(NSString *)fileName thumb:(NSInteger)thumb success:(void (^)(UIImage *img))success failure:(void (^)(NSError *error))failure
{
NSURLSessionDataTask *redirect = nil;
NSUserDefaults *info = [NSUserDefaults standardUserDefaults];
NSString *token = [info stringForKey:@"TOKEN"];
NSString *tokenStr = [NSString stringWithFormat:@"Token %@", token];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
configuration.HTTPAdditionalHeaders = @{@"Accept": @"application/json",
@"Content-type": @"application/json",
@"Authorization": tokenStr
};
configuration.discretionary = YES;
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSString *filePath = nil;
if (0 == thumb) {
filePath = [NSString stringWithFormat:@"%s%@?filename=%@",SERVER_ADDR, api, fileName];
}
else{
filePath = [NSString stringWithFormat:@"%s%@?filename=%@&thumb=%ld",SERVER_ADDR, api,fileName,(long)thumb];
}
NSURL *URL = [NSURL URLWithString:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
redirect = [manager dataTaskWithRequest:request
completionHandler:^(NSURLResponse *response, id responseObject, NSError *error){
NSURLSessionConfiguration *downloadConfig = [NSURLSessionConfiguration ephemeralSessionConfiguration];
downloadConfig.allowsCellularAccess = YES;
AFURLSessionManager *download = [[AFURLSessionManager alloc] initWithSessionConfiguration:downloadConfig];
//download.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
NSURL *realUrl = [response valueForKey:@"URL"];
NSMutableURLRequest *fileRequest = [NSMutableURLRequest requestWithURL:realUrl];
NSURLSessionDownloadTask *downloadTask = [download downloadTaskWithRequest:fileRequest
progress:nil
destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
}
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
NSLog(@"File downloaded error = %@",error);
if (failure) {
failure(error);
}
}
else{
NSLog(@"File downloaded to: %@", filePath);
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:filePath]];
if (success) {
success(image);
}
}
}];
[downloadTask resume];
}];
[redirect resume];
}
答案 0 :(得分:0)
我不知道我是否理解正确,但是,如果您想知道为什么downloadConfig
有Authorization
标题,那么您可以尝试通过替换{{1 }}属性就像使用HTTPAdditionalHeaders
一样(无论是通过获取当前标头并删除configuration
字段,还是将所有标头设置为Authorization
)。