2013-02-15 82 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