zoukankan      html  css  js  c++  java
  • IOS允许AFNetworking支持https请求

    ios 9出来以后,苹果推荐使用https协议,来提高数据传输之间的安全性,下面将介绍如何在工程里面配置,让AF支持https请求。

    一,证书准备

    • 1.证书转换

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

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

    二,代码修改

    • 先在的info.plist中,增加如下图的配置

      的plist文件配置

    • 使用XML格式配置plist文件内容如下:
      <key>NSAppTransportSecurity</key>
          <dict>
              <key>NSAllowsArbitraryLoads</key>
              <true/>
          </dict>

    三,使用系统类发送网络请求篇

    • 3.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请求了。

    • 3.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); }

    四。使用AFNetWorking发送网络请求

    •  4.1 AFNetWorking 2.x版本

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

      考虑到这个版本,我们还可以使用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];
     
    • 4.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];
  • 相关阅读:
    新版Unity的一些小玩意儿
    Three.js踩坑笔记
    Unity 接入腾讯云COS对象存储
    通过贝塞尔曲线编辑曲线
    利用ZXing.Net生成和识别二维码
    开源免费的安卓投屏工具-Scrcpy
    Unity Reflection Probe使用入门
    Unity VFX踩坑
    windows上通过自定义后缀文件启动Unity应用
    应用于Oculus Quest的VR头显应用
  • 原文地址:https://www.cnblogs.com/lxlx1798/p/15572636.html
Copyright © 2011-2022 走看看