• 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 不需要处理,本身就处理了,查了下貌似是的:

  • 相关阅读:
    纪念我用word发布的第一篇文章
    第一个SpringMVCHelloWorld
    JSTL学习笔记
    bonecp的使用
    hdu 1556 树状数组
    hdu 1561 树形DP
    MYSQL使用笔记
    Android中简单实现Spinner的数据绑定
    Android中利用Application实现多个Activity间共享数据
    技术到底重要不重要?
  • 原文地址:https://www.cnblogs.com/air-liyan/p/6407011.html
Copyright © 2020-2023  润新知