【问题标题】:How to remove the the http header option in AFNetworking?如何删除 AFNetworking 中的 http 标头选项?
【发布时间】: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

标签: ios afnetworking nsurlsession


【解决方案1】:

我不知道我是否理解正确,但是,如果您想知道为什么您的 downloadConfig 有一个 Authorization 标头,那么您可以尝试通过替换 HTTPAdditionalHeaders 属性来删除它,就像您一样对configuration 进行了处理(无论是通过获取当前标头并删除Authorization 字段,还是将所有标头设置为nil)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2010-09-21
    • 2010-12-28
    • 2011-05-26
    • 2018-04-24
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多