zoukankan      html  css  js  c++  java
  • NSURLSeesion和AFN发送https请求

    NSURLSession

    -(void)session
    {
        //1.创建会话对象
        NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
        
        //2.创建url
        NSURL* url=  [NSURL URLWithString:@"https://kyfw.12306.cn/otn/"];
        
        //    NSURL* url=  [NSURL URLWithString:@"https://www.apple.com"];haomagege20k
        [[session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
            NSLog(@"%@---%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding],error);
        }] resume];
    }
    
    
    #pragma mark ----------------------
    #pragma mark NSURLSessionDataDelegate
    /*
     challenge:挑战,质询
     NSURLAuthenticationMethodServerTrust:服务器信任授权
     */
    -(void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
    {
        NSLog(@"服务器信任%@",challenge.protectionSpace);
        
        if (![challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            return;
        }
        
        /*
         NSURLSessionAuthChallengeUseCredential = 0,  使用这个证书
         NSURLSessionAuthChallengePerformDefaultHandling = 1,  默认的处理  忽略                         /*
         NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2,会取消请求,忽略
         NSURLSessionAuthChallengeRejectProtectionSpace = 3,  拒绝,以后再次询问
         */
        NSURLCredential  *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        
        //第一个参数:如何处理这个证书(质询)
        completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        
    }
    

    AFN

    -(void)afn
    {
        
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSURL* url=  [NSURL URLWithString:@"https://kyfw.12306.cn/otn/"];
        
        //设置解析方式
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
        manager.securityPolicy.allowInvalidCertificates = YES;//是否允许过期的证书
        manager.securityPolicy.validatesDomainName = NO;//是否域名验证
        
        [[manager dataTaskWithRequest:[NSURLRequest requestWithURL:url] completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
            
            NSLog(@"%@---%@",[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding],error);
        }] resume];
    }
    
  • 相关阅读:
    My97 DatePicker 的jQuery插件
    UITableView 的 delegate2个基本方法
    你还在使用myImage = [UIImage imageNamed:@"icon.png"];吗
    自动生成数学题型三 (框架Struts2)题型如 a+b=c(a、b、c都为分数)
    自动生成数学题型二(框架struts2)题型如((a+b)*c=d)
    自动生成数学题型一 (框架Struts2) 题型如(a+b=c)
    创业团队应具备的要素
    在服务器中linux的centos系统如何封ip
    创业成功:必须具备的12个要点
    PHP+mysqli如何连接MySQL数据库,MySQL, Improved
  • 原文地址:https://www.cnblogs.com/xzk-it/p/6610634.html
Copyright © 2011-2022 走看看