使用AFHTTPRequestOperation进行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];
}];

编辑:

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

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];

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用2个不同的表进行身份验证

来自分类Dev

在iOS上使用OAuth2进行身份验证

来自分类Dev

使用2个不同的表进行身份验证

来自分类Dev

在iOS上使用OAuth2进行身份验证

来自分类Dev

如何通过身份验证方法FORM使用Jersey客户端(2.x)进行身份验证

来自分类Dev

如何在angular 2中进行身份验证

来自分类Dev

Twitter API 1.1无法对您进行身份验证2

来自分类Dev

自动进行OAUTH2身份验证

来自分类Dev

在AngularJS 2中对RESTful Web服务进行身份验证

来自分类Dev

想要的:使用AFNetworking-2使用基本身份验证的JSON / POST的最新示例

来自分类Dev

使用play2-auth检查身份验证

来自分类Dev

使用Laravel 4的Sentry 2,请记住身份验证

来自分类Dev

使用Angular2的NTLM身份验证

来自分类Dev

Github使用2因子身份验证创建令牌

来自分类Dev

Bitbucket身份验证以使用LibGit2Sharp

来自分类Dev

API身份验证和OAuth2的使用

来自分类Dev

使用Symfony2和FOSUserBundle的iOS身份验证

来自分类Dev

使用SASL的Python Cassandra 2身份验证

来自分类Dev

Bitbucket身份验证以使用LibGit2Sharp

来自分类Dev

API身份验证和OAuth2的使用

来自分类Dev

使用Ionic2与Firebase身份验证

来自分类Dev

使用AngularFire2的多种身份验证方法

来自分类Dev

使用AES进行DESFire身份验证

来自分类Dev

使用Glass进行身份验证

来自分类Dev

使用cURL进行设备身份验证

来自分类Dev

使用REST进行远程身份验证

来自分类Dev

使用JGit的PullCommand进行身份验证

来自分类Dev

使用Play进行身份验证!2.4

来自分类Dev

使用JWT进行身份验证