如何使用AFNetworking允许https连接?

Lou*_*ost 7 objective-c ios4 ios afnetworking

我已经设置了AFNetworking,但它不接受https网址.如何让AFNEtworking通过ssl进行连接.

我有以下代码:

  NSMutableURLRequest *apiRequest =
[self multipartFormRequestWithMethod:@"POST"
                                path: pathstr
                          parameters: params
           constructingBodyWithBlock: ^(id <AFMultipartFormData>formData)
           {
               //TODO: attach file if needed
           }];



AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest: apiRequest];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    //success!
    completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    //failure :(

    NSLog(@"%@", error);
    completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]);
}];

[operation start];
Run Code Online (Sandbox Code Playgroud)

小智 14

operation.securityPolicy.allowInvalidCertificates = YES;
Run Code Online (Sandbox Code Playgroud)

这段代码非常重要.如果你不添加它,你会收到一个错误.


Hol*_*rin 6

这显然只有在您拥有非自签名证书或您添加时才有效:

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_到你的pch文件.如果你正在使用可可豆荚,你可能需要继承AFHTTPRequestOperation并实现:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {

        if ([self bypassSslCertValidation:protectionSpace])
            return YES;
        else
            return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];

    }
    return [super connection:connection canAuthenticateAgainstProtectionSpace:protectionSpace];

}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        if ([self bypassSslCertValidation:challenge.protectionSpace]) {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
            return;
        }
        else
            return [super connection:connection didReceiveAuthenticationChallenge:challenge];
        return;
    }
}

- (BOOL) bypassSslCertValidation:(NSURLProtectionSpace *) protectionSpace
{
    if (ENVIRONMENT_TYPE == DEV_ENV || ENVIRONMENT_TYPE == STAGING_ENV) {
        return YES;
    }
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

然后告诉AFNEtworking使用新的子类:

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];
Run Code Online (Sandbox Code Playgroud)

这不是世界上最简单的事情,技术上忽略自签名并不能使它工作,但是如果你使用标准的SLL证书很可能它会正常工作,记得删除这些代码或使其仅在调试时可用如果你打算发布.

添加回答,因为评论有char限制!

很少有选择查看标题

可以手动添加到队列的返回操作:

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest
Run Code Online (Sandbox Code Playgroud)

或者将您的自定义子类操作传递给此:

- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
Run Code Online (Sandbox Code Playgroud)