如何删除 AFNetworking 中的 http 标头选项?
Posted
技术标签:
【中文标题】如何删除 AFNetworking 中的 http 标头选项?【英文标题】:How to remove the the http header option in AFNetworking? 【发布时间】:2015-01-31 09:10:53 【问题描述】:如果调用下载函数,在Http Header中设置“Authorization”,并调用第二个函数“downloadTaskWithRequest”,但http头中还包含“ Authorization”选项,从代码中,我没有设置选项,如何在调用第二个函数之前删除“Authorization”选项,谢谢!
- (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];
【问题讨论】:
似乎 ephemeralSessionConfiguration 被重用了。 Apple 文档说“当您的应用程序使会话无效时,所有临时会话数据都会自动清除。”这向我表明,您需要在设置第二个会话之前使您的第一个会话无效。或者您可以尝试设置 downloadConfig.HTTPAdditionalHeaders = nil 【参考方案1】:我不知道我是否理解正确,但是,如果您想知道为什么您的 downloadConfig
有一个 Authorization
标头,那么您可以尝试通过替换 HTTPAdditionalHeaders
属性来删除它,就像您一样对configuration
进行了处理(无论是通过获取当前标头并删除Authorization
字段,还是将所有标头设置为nil
)。
【讨论】:
以上是关于如何删除 AFNetworking 中的 http 标头选项?的主要内容,如果未能解决你的问题,请参考以下文章
objective-c 如何使用 AFNetworking 使用 NSURLSession
如何通过 AFNetworking 创建简单的 Http 请求
如何使用 AFNetworking 为请求设置 HTTP 正文?
如何使用 AFNetworking 2 设置 HTTP 请求?