ios - 使用 AFHTTPRequestOperation 进行 AFNetworking 2 身份验证

标签 ios afnetworking-2

我想在批处理操作中使用 AFNetworking。我想下载 3 个 json 文件。

如何使用 AFHTTPRequestOperation 添加基本身份验证?

NSMutableArray *mutableOperations = [NSMutableArray array];

for (NSString *fileURL in filesToDownload) {
    NSURLRequest *request = [NSURLRequest 
                                requestWithURL:[NSURL URLWithString:fileURL]];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                                initWithRequest:request];
    [operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
                                                , id responseObject) {
        NSLog(@"success: %@", operation.responseString);
    }
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);
    }];
    [mutableOperations addObject:operation];
}

NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations 
                                    progressBlock:^(NSUInteger numberOfFinishedOperations
                                        , NSUInteger totalNumberOfOperations) {
    NSLog(@"%d of %d complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
    NSLog(@"All operations in batch complete");
}];

[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];

非常感谢

最佳答案

使用 AuthenticationChallengeBlock 处理基本的身份验证挑战。

[operation setAuthenticationChallengeBlock:
        ^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
   NSURLCredential *cred = [NSURLCredential 
    credentialWithUser:@"username" 
    password:@"password" 
    persistence:NSURLCredentialPersistenceForSession];

   [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge];
}];

编辑:

另一种选择是在请求 header 中传递身份验证。

NSURLMutableRequest *request = [NSURLMutableRequest
                                requestWithURL:[NSURL URLWithString:fileURL]];
NSData* authData = [[[NSString 
    stringWithFormat:@"username:password"] 
        stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] 
            dataUsingEncoding: NSASCIIStringEncoding];
NSString *finalAuth = [authData base64EncodedString];
finalAuth = [NSString stringWithFormat:@"Basic %@",finalAuth];
[request setValue:finalAuth forHTTPHeaderField:@"Authorization"];

另一种解决方案:

NSURLCredential *credential = [NSURLCredential 
    credentialWithUser:@"login" 
    password:@"password" 
    persistence:NSURLCredentialPersistenceForSession];

[operation setCredential:credential];

关于ios - 使用 AFHTTPRequestOperation 进行 AFNetworking 2 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19587453/

相关文章:

ios - AF网络可达性ForLocalWiFi

ios - 在 viewDidLoad 中执行对服务器的调用

ios - AFNetworking 2.0 - 强制缓存

ios - 使用 AFHTTPRequestOperation Manager 时响应失败

iOS - 在 AFNetworking 异步图像下载线程完成后重新加载 UITableView

ios - UISwipeGestureRecognizer 工作异常

ios - iOS 14 上的 flutter FCM 7

ios - 对 Storyboard感到困惑并以编程方式推送 ios Objective C

ios - 如何在 Swift 中集成 Google reCAPTCHA

ios - 将整数上传到 Firebase 数据库 IOS 的按钮