美文网首页
39.iOS使用HTTPS

39.iOS使用HTTPS

作者: bytebytebyte | 来源:发表于2020-11-11 00:02 被阅读0次
1.针对AFN

(1)导出证书

(2)在原来http请求加一句话就OK了

(3)调用Get请求

// Get请求
+ (void)getValueWithGetUrl:(NSString *)url parameters:(NSDictionary *)parameters complete:(completes)complete;
{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", nil];
    [manager setSecurityPolicy:[self customSecurityPolicy]];
    [manager GET:kUrlstr parameters:parameters
         progress:nil
          success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
              complete(responseObject);
          }
          failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
              SLog(@"%@",[error description]);
              complete(@"0");
          }];
}
+ (AFSecurityPolicy*)customSecurityPolicy {
    // /先导入证书
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"ca" ofType:@"cer"];//证书的路径
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    // AFSSLPinningModeCertificate 使用证书验证模式
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    // allowInvalidCertificates 是否允许无效证书(也就是自建的证书),默认为NO
    securityPolicy.allowInvalidCertificates = YES;
    //validatesDomainName 是否需要验证域名,默认为YES;
    securityPolicy.validatesDomainName = NO;
//    securityPolicy.validatesCertificateChain = NO;
    securityPolicy.pinnedCertificates = [NSSet setWithObjects:certData, nil];
    return securityPolicy;
}


2.针对NSURLSession下载

此方法实际上是手动安装证书,不需要倒入证书的;这也是系统方法有点的体现,缺点就是下载有点慢

#define kUrlstrs @"https://192.168.1.159:9091/admin/api.jsp"

-(void)downLoad
{
    WS(weakSelf);
    NSString *paramPath = [NSString stringWithFormat:@"?method=loadAvatar&username=18337125561"];
    NSURL *url = [NSURL URLWithString:[kUrlstrs stringByAppendingString:paramPath]];
    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:[[NSOperationQueue alloc]init]];
    NSURLSessionDataTask *dataTask = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSLog(@"***:%@ %@",data,error);
        weakSelf.imageV.image = [UIImage imageWithData:data];
    }];
    [dataTask resume];
}


遵循下协议NSURLSessionDelegate,上边已经设置了代理,下边是实现方法.

- (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告诉服务器信任证书
        if (completionHandler) {
            completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
        }
    }
    //    NSLog(@"protectionSpace = %@",challenge.protectionSpace);
}


3.解决SDWebImage的HTTPS问题

 NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)解决办法

[self.imageV sd_setImageWithURL:[NSURL URLWithString:@"https://192.168.1.159:9091/admin/api.jsp?method=loadAvatar&username=18805025104"] placeholderImage:[UIImage imageNamed:@"jsy.jpg"] options:SDWebImageAllowInvalidSSLCertificates];



相关文章

  • 39.iOS使用HTTPS

  • 使用HTTPS

    免费SSL 沃通在2016年9月29日凌晨零点停止签发免费SSL证书,重新恢复的时间还不确定。 StartSSL无...

  • Retrofit使用https

    最近CTO使用第三方app评测工具检测到项目中没有使用https。接口的请求地址都是https的只是都设置了忽略证...

  • frps https使用

    原文地址 frps https使用 申请免费证书 这里可以在网上找一个免费的https证书, 生成之后下载到本地 ...

  • django使用https

    使用 runserver 是不能使用 https 的 解决办法:使用 runsslserver 步骤: 1.安装:...

  • Android使用https

    前言 HI,欢迎来到裴智飞的《每周一博》。今天是九月第三周,我给大家介绍一下安卓如何使用https。 HTTP协议...

  • Android 使用 HTTPS

    如果你的项目的网络框架是okhttp,那么使用https还是挺简单的,因为okhttp默认支持HTTPS。传送门 ...

  • afn 使用https

    苹果最近出台新规,从2017年1月1日起,iOS 就告别http了,下边是afn使用https的一些步骤. 1.c...

  • Swift使用HTTPS

    1. HTTPS到底是个什么鬼? SSL/TSL+HTTP就是在HTTP传输之前,先给数据做非对称加密,客户端用公...

  • Android使用Https

    OkHttp使用Https(OkHttp版本3.8.1) 一、使用(4步搞定) 1.初始化OkHttpClient...

网友评论

      本文标题:39.iOS使用HTTPS

      本文链接:https://www.haomeiwen.com/subject/yozfmktx.html