原文地址:http://blog.5ibc.net/p/100221.html

众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https
楼主正好近日将http转为https,给还没动手的朋友分享一二

一、证书准备
1、证书转换

在服务器人员,给你发送的crt证书后,进到证书路径,执行下面语句
// openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der
这样你就可以得到cer类型的证书了。双击,导入电脑。

2、证书放入工程

1、可以直接把转换好的cer文件拖动到工程中。
2、可以在钥匙串内,找到你导入的证书,单击右键,导出项目,就可以导出.cer文件的证书了

二、代码准备

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>

1.1 NSURLConnection设置支持https。

在2015年iOS9的更新中,NSURLConnection 被废弃 由 NSURLSession 取代,所以本身是不建议大家继续用这个类做网络请求的(同样也有AFNetWorking 2.x版本),但是考虑到一些旧程序,也不能说改就改,说替换就替换的,所以还是需要普及一下,如果用到了NSURLConnection你需要怎么做。

代码如下:

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告诉服务器,客户端信任证书
// 创建凭据对象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 告诉服务器信任证书
[challenge.sender useCredential:credntial forAuthenticationChallenge:challenge];
}
}

你只需要简单的,添加上如上的代理方法,就可以在不影响你原有请求的基础上,增加了https请求的支持了。

1.2 NSURLSession设置支持https。

现在推荐使用的就是NSURLSession来处理相关的网络请求了,如果使用系统自带的类,可以参考如下代码:

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task  didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
// 判断是否是信任服务器证书
if(challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
// 告诉服务器,客户端信任证书
// 创建凭据对象
NSURLCredential *credntial = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 通过completionHandler告诉服务器信任证书
completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
}
NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}

2.使用AFNetWorking发送网络请求篇

AFNetworking是一个讨人喜欢的网络库,适用于iOS以及Mac OS X. 它构建于在NSURLConnection, NSOperation, 以及其他熟悉的Foundation技术之上. 它拥有良好的架构,丰富的api,以及模块化构建方式,使得使用起来非常轻松.。

2.1 AFNetWorking 2.x版本

考虑到这个版本,我们还可以使用AFHTTPRequestOperationManager这个类来处理网络请求。所以我们要做的就是给这个类,设置一些参数,让它可以支持https的请求,代码如下:
支持https(校验证书,不可以抓包):

// 1.初始化单例类
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.设置证书模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
mgr.securityPolicy.pinnedCertificates = [[NSArray alloc] initWithObjects:cerData, nil];
// 客户端是否信任非法证书
mgr.securityPolicy.allowInvalidCertificates = YES;
// 是否在证书域字段中验证域名
[mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校验证书,可以抓包查看):

    // 1.初始化单例类
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
mgr.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.设置非校验证书模式
mgr.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
mgr.securityPolicy.allowInvalidCertificates = YES;
[mgr.securityPolicy setValidatesDomainName:NO];

2.2 AFNetWorking 3.x版本

在Xcode7.0之后,苹果废弃了NSURLConnection方法,数据请求使用NSURLSession,作为网络请求类第三方库使用量最大的AFN也及时的更新的新的版本——AFN 3.0版本。新的版本的里废弃了基于NSURLConnection封装的AFHTTPRequestOperationManager,转而使用基于NSURLSession封装的AFHTTPSessionManager了。
支持https(校验证书,不可以抓包):

// 1.初始化
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.securityPolicy.SSLPinningMode = AFSSLPinningModeCertificate;
// 2.设置证书模式
NSString * cerPath = [[NSBundle mainBundle] pathForResource:@"xxx" ofType:@"cer"];
NSData * cerData = [NSData dataWithContentsOfFile:cerPath];
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate withPinnedCertificates:[[NSSet alloc] initWithObjects:cerData, nil]];
// 客户端是否信任非法证书
mgr.securityPolicy.allowInvalidCertificates = YES;
// 是否在证书域字段中验证域名
[mgr.securityPolicy setValidatesDomainName:NO];

支持https(不校验证书,可以抓包查看):

// 1.初始化
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// 2.设置非校验证书模式
manager.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeNone];
manager.securityPolicy.allowInvalidCertificates = YES;
[manager.securityPolicy setValidatesDomainName:NO];

到这里配置就完成了,希望对你有所帮助。

最新文章

  1. java springMVC SSM 操作日志 4级别联动 文件管理 头像编辑 shiro redis
  2. Linux ls
  3. Javascript提升阶段学习
  4. python 安装扩展库
  5. HDU 5800 (DP)
  6. gpg --verify之&quot;Can&#39;t check signature: No public key&quot;
  7. [改善Java代码]让多重继承成为现实
  8. 斐波那契fib
  9. 四轴飞行器1.4 姿态解算和Matlab实时姿态显示
  10. 关于jQuery的条件判断问题
  11. VM VirtrualBox 安装centos6.5后的网络设置
  12. linux 管理权限
  13. 冲刺NO.7
  14. mysql进阶(五)数据表中带OR的多条件查询
  15. 2018-2019-2 网络对抗技术 20165311 Exp3 免杀原理与实践
  16. 整合Solr与tomcat以及第一个core的配置
  17. SAP按销售订单生产和标准结算配置及操作手册
  18. nginx配置文件nginx.conf 不包括server节点
  19. JavaScript 空位补零实现代码
  20. Ubuntu 10.04 安装 Oracle11gR2

热门文章

  1. SQL INTERSECT
  2. xcode_6_beta.dmg
  3. HDU 1595 find the longest of the shortest【次短路】
  4. keytool命令总结
  5. MFC用代码加入对话框背景图片和button图片
  6. Python——Shell编程关于Sha-Bang(#!)
  7. Apache Ant和Apache Maven的区别
  8. BeanUtils的介绍使用
  9. 在 Ubuntu12.04/Xubuntu12.04 上安装 QQ2012,这才是真正可行的
  10. Android访问php webservice