博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AFNetworking、ASIHTTPRequest中SSL的使用
阅读量:5963 次
发布时间:2019-06-19

本文共 2737 字,大约阅读时间需要 9 分钟。

首先介绍下AFNetworking中的使用:

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

AFHTTPSessionManager为例,代码例如以下:

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) {        //失败信息    }];
AFHTTPRequestOperationManager为例,代码例如以下:
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) {        //失败信息    }];
以下来讲下ASI,

ASIFormDataRequest为例。代码例如以下:

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];

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

演示
http://blog.iamzsx.me/show.html?

id=155002

转载地址:http://rijax.baihongyu.com/

你可能感兴趣的文章
Java动态代理--CGLib动态代理
查看>>
工作日志2009年、一
查看>>
隔行变色,移入变色和轮播
查看>>
通过实例来学习使用Linux KVM
查看>>
squid构建代理服务器
查看>>
共享模式和专有模式详解
查看>>
Sql Server系列:自定义函数
查看>>
我的友情链接
查看>>
java集合
查看>>
server2008R2升级域后的安全策略
查看>>
linux应急处置案例学习
查看>>
CCNP学习之路之AAA详解2
查看>>
influxdb 安装配置详解
查看>>
greenplum(5.10)生产系统主备节点切换
查看>>
aspack的简单脱壳,望大牛勿喷。
查看>>
经典sql语句
查看>>
我的友情链接
查看>>
CentOS 7.0编译安装Nginx1.6.0+MySQL5.6.19+PHP5.5.14
查看>>
amazon 设计 10 puzzle.
查看>>
利用单臂路由实现VLAN间路由
查看>>