码迷,mamicode.com
首页 > Web开发 > 详细

AFNetworking、ASIHTTPRequest中SSL的使用

时间:2014-07-31 20:52:47      阅读:287      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   使用   os   io   for   art   

首先介绍下AFNetworking中的使用:

2.0要注意个地方:IOS7及其以后,采用AFHTTPSessionManagerIOS7之前采用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 = [NSData dataWithContentsOfFile:certificatePath];
    
    AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
    [securityPolicy setAllowInvalidCertificates:YES];
    [securityPolicy setPinnedCertificates:@[certificateData]];
    [securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];
    
    [httpClient setSecurityPolicy:securityPolicy];
    
    [httpClient GET:@"http://xxxx" parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        //失败信息
    }];</span>

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,代码就不贴了。

AFNetworking、ASIHTTPRequest中SSL的使用,布布扣,bubuko.com

AFNetworking、ASIHTTPRequest中SSL的使用

标签:style   http   color   使用   os   io   for   art   

原文地址:http://blog.csdn.net/quanqinyang/article/details/38320781

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!