2013-02-15 96 views
7

我有AFNetworking设置,但它不接受https网址。我怎样才能通过SSL连接AFNEtworking。如何使用AFNetworking允许https连接?

我有以下代码:

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

报道在日志什么错误?另外,你的服务器证书是否有效? – nioq 2013-02-15 18:23:50

回答

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证书它很可能会工作得很好,请记住删除此代码或仅在调试时才可用,如果您计划发布。

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

几个选择看,可以手动添加到队列中的headers

返回操作:

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

或者在您的自定义子类的操作传递到这一个:

- (void)enqueueHTTPRequestOperation:(AFHTTPRequestOperation *)operation; 
+1

有没有更简单的方法来做到这一点? – Jasmine 2013-02-15 20:50:34

+0

不幸的是,我没有发现,我的字面意思就是昨天为我的本地SSL开发实现了这一点,甚至可以告诉它哪些主机忽略SSL。我听说他们添加了SSL Pinning,我会为它打google。 – Holyprin 2013-02-15 21:53:49

+0

我在哪里告诉AfNetworking实现AFHTTPClient子类?如何更改我的代码以执行此操作...谢谢! – Jasmine 2013-02-16 01:01:03

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

这将允许您使用https连接,但不会成为安全连接。这意味着您需要使用来自Web服务的* .cer证书设置securityPolicy以建立安全连接。 – 2013-12-08 19:22:04

14
operation.securityPolicy.allowInvalidCertificates = YES; 

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

+0

这是完美的,更干净。 – Jirapong 2014-07-21 08:33:37

0

如果AFNetworking被禁用的子类在超实现,那么只是把这样的代码:访问http // // foo的酒吧和设置此为bool