AFNetworking、ASIHTTPRequest中SSL的使用

首先介绍下AFNetworking中的使用:

2.0要注意个地方:IOS7及其以后,采用AFHTTPSessionManagerIOS7之前采用AFHTTPRequestOperationManager

以AFHTTPSessionManager为例,代码如下:

[objc]  view plain copy
  1. <span style="font-size:14px;">AFHTTPSessionManager   *httpClient = [AFHTTPSessionManager manager];  
  2.     httpClient.requestSerializer = [AFHTTPRequestSerializer serializer];  
  3.     httpClient.responseSerializer = [AFHTTPResponseSerializer serializer];  
  4.       
  5.     // SSL Pinning  
  6.     NSString *certificatePath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"];  
  7.     NSData *certificateData = [NSData dataWithContentsOfFile:certificatePath];  
  8.       
  9.     AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];  
  10.     [securityPolicy setAllowInvalidCertificates:YES];  
  11.     [securityPolicy setPinnedCertificates:@[certificateData]];  
  12.     [securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];  
  13.       
  14.     [httpClient setSecurityPolicy:securityPolicy];  
  15.       
  16.     [httpClient GET:@"http://xxxx" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {  
  17.           
  18.     } failure:^(NSURLSessionDataTask *task, NSError *error) {  
  19.         //失败信息  
  20.     }];</span>  

AFHTTPRequestOperationManager为例,代码如下:
[objc]  view plain copy
  1. <span style="font-size:14px;">AFHTTPRequestOperationManager *httpClient1 = [AFHTTPRequestOperationManager manager];  
  2.     httpClient1.requestSerializer = [AFHTTPRequestSerializer serializer];  
  3.     httpClient1.responseSerializer = [AFHTTPResponseSerializer serializer];  
  4.     // SSL Pinning  
  5.     NSString *certificatePath1 = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"];  
  6.     NSData *certificateData1 = [NSData dataWithContentsOfFile:certificatePath1];  
  7.       
  8.     AFSecurityPolicy *securityPolicy1 = [[AFSecurityPolicy alloc] init];  
  9.     [securityPolicy1 setAllowInvalidCertificates:YES];  
  10.     [securityPolicy1 setPinnedCertificates:@[certificateData1]];  
  11.     [securityPolicy1 setSSLPinningMode:AFSSLPinningModeCertificate];  
  12.       
  13.     [httpClient setSecurityPolicy:securityPolicy1];  
  14.       
  15.     [httpClient1 GET:@"http://xxxx" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {  
  16.         //成功信息  
  17.     } failure:^(AFHTTPRequestOperation *operation, NSError *error) {  
  18.         //失败信息  
  19.     }];</span>  


下面来讲下ASI,

ASIFormDataRequest为例,代码如下:

[objc]  view plain copy
  1. <span style="font-size:14px;">ASIFormDataRequest * request;  
  2.     request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@""]];  
  3.     NSData *cerFile = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"d1" ofType:@"cer"]];  
  4.     SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)cerFile);  
  5.     NSArray *array = [NSArray arrayWithObjects:(__bridge id)cert,nil];  
  6.     [request setClientCertificates:array];  
  7.     [request setValidatesSecureCertificate:NO];  
  8.       
  9.     [request startSynchronous];</span>  



ASIHTTPRequest的参考http://www.tuicool.com/articles/UN3AveR,代码就不贴了。

演示 点击打开链接
http://blog.iamzsx.me/show.html?id=155002

你可能感兴趣的:(AFNetworking、ASIHTTPRequest中SSL的使用)