2012-10-19 34 views
26

我想允許無效的SSL證書。 我的主要代碼如下:我想允許無效的SSL證書與AFNetworking

myClient = [[MyClient alloc] init]; 
[myClient getHtml:@"/path/to/the/distination.html"]; 

的MyClient類代碼如下:

#import <Foundation/Foundation.h> 
#import "AFNetworking.h" 

@interface MyClient : NSObject 

- (void)getHtml:(NSString *)path; 

@end 

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 

@implementation MyClient 

- (void)getHtml:(NSString *)path 
{ 
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"https://trusted.server.net"]]; 
    [httpClient getPath:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     NSLog(@"%@", responseObject); 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     NSLog(@"error = %@", error); 
    }]; 
} 

@end 

我閱讀頁面下方,並試圖宏觀但這不起作用。

自簽名證書的SSL·問題#189·AFNetworking/AFNetworkinghttps://github.com/AFNetworking/AFNetworking/issues/189

請幫我...

回答

40

允許帶有AFNetworking的無效SSL證書。下面添加#進口Availability.h

#define _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ 1 
+13

從版本1.2.1開始,您不需要再設置此定義。相反,您可以在AFHTTPRequestOperation上將'allowsInvalidSSLCertificate'屬性設置爲YES,這非常方便。 – Phil

+0

如果你這樣做,確保你只在調試模式下進行。如果您打到HTTPS端點,並且您允許無效的證書,那麼您將失去SSL獲得的安全性的很大一部分。 –

+6

有沒有什麼辦法讓allowedInvalidSSLCertificate與UIImageView + AFNetworking一起使用? –

8

請注意,如果您通過的CocoaPods安裝,#define -ing項目中的這個宏將不夠 - 在編譯靜態庫以使其生效時必須設置編譯器宏。

+2

「編譯器宏必須爲了讓它生效編譯靜態庫時,可以設置」 - 你怎麼做? –

4

你應該繼承的AFHttpClient,並覆蓋

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)request 
               success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success 
               failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure; 

這是我的代碼。

- (AFHTTPRequestOperation *)HTTPRequestOperationWithRequest:(NSURLRequest *)urlRequest success:(void (^)(AFHTTPRequestOperation *, id))success failure:(void (^)(AFHTTPRequestOperation *, NSError *))failure 
{ 
    AFHTTPRequestOperation *operation = [super HTTPRequestOperationWithRequest:urlRequest success:success failure:failure]; 

    [operation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) { 
     return YES; 
    }]; 
    [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) { 
     if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) { 
      [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; 
     } 
    }]; 
    return operation; 
} 

你使用這個httpclient,它可以成功訪問自簽名的web。

+0

爲什麼你需要爲此進行子類化?似乎你可以從任何地方添加保護空間和身份驗證挑戰塊,或者我錯過了什麼? –

+0

如果要以編程方式允許自簽名證書並仍使用AFHTTPClient,則需要這樣做。不幸的是,AFHTTPClient沒有相應的'setAuthenticationAgainstProtectionSpaceBlock'。 – bobics

24

在AFURLConnectionOperation.h以下行,您現在可以使用AFHTTPClient的allowsInvalidSSLCertificate財產。無需在最新版本的AFNetworking中使用定義。

AFHTTPClient* client = [AFHTTPClient clientWithBaseURL:@"url"]; 
client.allowsInvalidSSLCertificate = YES; //this defaults to no 
+0

不適用於AFnetworking版本2.0之前 – NAlexN

1

This是允許使用AFNetworking的無效SSL證書的最佳方法。

use add a _AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_ to

project > Build Settings > Preprocessor Macros and add it to Debug entry.

希望它有助於

12

我使用RestKit所以

client.allowsInvalidSSLCertificate = YES; 

不起作用。該選項不會傳播到由restkit創建的操作。

我正在使用cocoapods,因此pch文件或pod項目中的任何更改都會被重寫。 我一直在使用的「hack」是一個cocoapod後安裝操作,它添加了所需的預處理器定義。在我的pod文件結尾添加了:

post_install do |installer_representation| 
    installer_representation.project.targets.each do |target| 
    if target.name == 'Pods-AFNetworking' 
     target.build_configurations.each do |config| 
     config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= ['$(inherited)'] 
     config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] << '_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1' 
     end 
    end 
    end 
end 
+1

在iOS7和XCode 5上,這對我來說是個竅門。我正在使用AFNetworking 1.3.3 – Humber

+0

OMG!非常感謝! –

+0

這也幫助了我。好東西。 – bleeckerj

44

在AFNetworking 2。0,你可以使用以下命令:

[AFHTTPRequestOperationManager manager].securityPolicy.allowInvalidCertificates = YES; 
+0

這有幫助。我添加了「self.securityPolicy.allowInvalidCertificates = YES;」在AFHTTPRequestOperationManager init方法中。 –

1

,如果你使用的是使用RestKit

client.allowsInvalidSSLCertificate = YES; 

將無法​​正常工作,而不是這樣做:

如果你手動添加其餘套件項目,點擊RestKit.xcodeproj進入項目>構建設置>預處理器宏

並添加_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1

即將完成。

+1

當前objectManager.HTTPClient.allowsInvalidSSLCertificate = YES;或RestKit中的其他類似工作 –

2

,如果你使用的是使用RestKit

client.allowsInvalidSSLCertificate = YES; 

將無法​​正常工作,而不是這樣做:

在你的項目中,點擊RestKit.xcodeproj轉到項目>構建設置>預處理宏

並添加_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_=1

這就是完成。

5

下面介紹如何使用POST操作的管理器進行操作。

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; //initialize 
manager.securityPolicy.allowInvalidCertificates=YES; //allow unsigned 
manager.responseSerializer=[AFJSONResponseSerializer serializer]; //set up for JSOn 
[manager POST:@"myweb.com" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) { 
    //success stuff 
}failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    //error stuff 
}]; 

編輯 - 迅速版本:

var securityPolicy = AFSecurityPolicy() 
    securityPolicy.allowInvalidCertificates = true; 
    manager.securityPolicy = securityPolicy; 
    manager.POST(
     "https://exmaple.com/foobar.php", 
     nil, 
    ... 
3

AFHTTPRequestOperation *操作= [[AFHTTPRequestOperation的alloc] initWithRequest:的URLRequest]; operation.securityPolicy.allowInvalidCertificates = YES;

0

我遇到了同樣的問題,沒有一個我讀過的解決方案。

對我來說,唯一的解決方法是設置AFSecurityPolicy這樣的:

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone]; 
securityPolicy.allowInvalidCertificates = YES; 
manager.securityPolicy = securityPolicy; 

我決定回答雖然問題是舊的,也許可以幫助別人。

4

我在我的代碼中擴展了AFHTTPSessionManager。當對一個測試服務器測試,所有我需要的是添加一行是這樣的:

mySessionManager.securityPolicy.allowInvalidCertificates = YES;