zoukankan      html  css  js  c++  java
  • iOS 绕过https证书验证 请求数据

    HTTPS和HTTP:
     1、https协议需要到ca申请证书,一般免费证书很少,需要交费。
     2、http是超文本传输协议,信息是明文传输,https 则是具有安全性的ssl加密传输协议。
     3、http和https使用的是完全不同的连接方式,用的端口也不一样
     4、http的连接很简单,是无状态的;HTTPS协议是由SSL HTTP协议构建的可进行加密传输、身份认证的网络协议,比http协议安全。

    因为项目需要调试,公司自己弄了个证书验证,所有需要在请求数据的时候绕过验证,在网上搜了下,最后代码贴上

    - (void)session{
        
        NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
        NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:Nil];
        NSURLSessionTask *task = [session dataTaskWithURL:[NSURL URLWithString:@"https://114.55.231.139/"] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            //
            
            NSLog(@"error:%@",error);
            NSLog(@"data:%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        }];
        [task resume];
        
        
    }
    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
        if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
            if([challenge.protectionSpace.host isEqualToString:@"114.55.231.139"]){
                NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
                completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
            } else {
                completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
            }
        }
    }

    有说AFNetworking 不需要处理,本身就处理了,查了下貌似是的:

  • 相关阅读:
    MySQL中默认值中用时间函数的问题
    mysql数据表的操作
    mysql数据库的基本操作
    mysql数据库的几个基本概念
    【转载】CentOS6.5_X64下安装配置MongoDB数据库
    Swap Swap,即交换分区
    linux中给PHP安装mongodb的扩展
    centos yum 安装 mongodb 以及php扩展
    设计模式主要分三个类型:创建型、结构型和行为型
    MySQL DELETE语句和TRUNCATE TABLE语句的区别
  • 原文地址:https://www.cnblogs.com/air-liyan/p/6407011.html
Copyright © 2011-2022 走看看