首页 > 代码库 > AFNetworking、ASIHTTPRequest中SSL的使用
AFNetworking、ASIHTTPRequest中SSL的使用
首先介绍下AFNetworking中的使用:
2.0要注意个地方:IOS7及其以后,采用AFHTTPSessionManager,IOS7之前采用AFHTTPRequestOperationManager。
以AFHTTPSessionManager为例,代码如下:
<span style="font-size:14px;">AFHTTPSessionManager *httpClient = [AFHTTPSessionManager manager]; httpClient.requestSerializer = [AFHTTPRequestSerializer serializer]; httpClient.responseSerializer = [AFHTTPResponseSerializer serializer]; // SSL Pinning NSString *certificatePath = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"]; NSData *certificateData = http://www.mamicode.com/[NSData dataWithContentsOfFile:certificatePath];>
以AFHTTPRequestOperationManager为例,代码如下:<span style="font-size:14px;">AFHTTPRequestOperationManager *httpClient1 = [AFHTTPRequestOperationManager manager]; httpClient1.requestSerializer = [AFHTTPRequestSerializer serializer]; httpClient1.responseSerializer = [AFHTTPResponseSerializer serializer]; // SSL Pinning NSString *certificatePath1 = [[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"der"]; NSData *certificateData1 = [NSData dataWithContentsOfFile:certificatePath1]; AFSecurityPolicy *securityPolicy1 = [[AFSecurityPolicy alloc] init]; [securityPolicy1 setAllowInvalidCertificates:YES]; [securityPolicy1 setPinnedCertificates:@[certificateData1]]; [securityPolicy1 setSSLPinningMode:AFSSLPinningModeCertificate]; [httpClient setSecurityPolicy:securityPolicy1]; [httpClient1 GET:@"http://xxxx" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) { //成功信息 } failure:^(AFHTTPRequestOperation *operation, NSError *error) { //失败信息 }];</span>
下面来讲下ASI,以ASIFormDataRequest为例,代码如下:
<span style="font-size:14px;">ASIFormDataRequest * request; request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@""]]; NSData *cerFile = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"d1" ofType:@"cer"]]; SecCertificateRef cert = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)cerFile); NSArray *array = [NSArray arrayWithObjects:(__bridge id)cert,nil]; [request setClientCertificates:array]; [request setValidatesSecureCertificate:NO]; [request startSynchronous];</span>ASIHTTPRequest的参考http://www.tuicool.com/articles/UN3AveR,代码就不贴了。
演示点击打开链接
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。