7

我设置了 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];
4

4 回答 4

14
operation.securityPolicy.allowInvalidCertificates = YES;

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

于 2014-03-10T07:54:34.267 回答
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;
}

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

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@""]];
[client registerHTTPOperationClass:[YourSubClassHTTPRequestOperation class]];

这不是世界上最容易做的事情,从技术上讲,忽略自签名并不能使它工作,但是如果您使用标准 SLL 证书它可能会正常工作,请记住删除此代码或使其仅在调试时可用如果你打算发布。

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

标题的选择很少

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

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest

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

- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation;
于 2013-02-15T20:20:36.413 回答
2

试试这个代码。

  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.securityPolicy.allowInvalidCertificates = YES;
[operation start];
于 2013-10-10T13:08:33.510 回答
0

如果 AFNetworking 在超类实现上被禁用子类,那么只需输入类似 :http//foo//bar 的代码并将其设置为 bool

于 2016-07-15T05:36:13.247 回答