zoukankan      html  css  js  c++  java
  • iOS 网络请求信任所有证书

    网络请求信任所有证书

    1、AFNetWorking 设置allowInvalidCertificates 即可

        

    AFHTTPSessionManager  *afnManager = [AFHTTPSessionManager manager];
     // 客户端是否信任非法证书
     afnManager.securityPolicy.allowInvalidCertificates = YES;
     // 是否在证书域字段中验证域名
     [afnManager.securityPolicy setValidatesDomainName:NO];

    2、ASIHTTPRequest 设置setValidatesSecureCertificate为NO

     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
           
     [request setValidatesSecureCertificate:NO];
     [request startSynchronous];

    3、使用原生NSURLSession,

     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]delegate:self delegateQueue:[NSOperationQueue mainQueue]];

    在改类中设置代理,遵循代理协议,实现代理方法

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler

    //请求方法
    NSString * urlstr = [NSString stringWithFormat:@"%@x x x x x x",HttpServerStr]; NSDictionary* parameters = @{@"loginname":_userNameTextField.text,@"password":_passWordTextField.text}; NSData *paramData = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:nil]; //访问请求 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlstr]]; request.timeoutInterval = TimeoutInterval; request.HTTPMethod = @"POST"; request.HTTPBody = paramData; [request setValue:[NSString stringWithFormat:@"application/x-www-form-urlencoded;charset=UTF-8"] forHTTPHeaderField:@"Content-Type"]; // NSURLSession *session = [NSURLSession sharedSession]; NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { NSLog(@"请求结束"); if (error == nil) { NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; _userManager.h5_user_token = [dic objectForKey:@"user_token"]; NSLog(@"h5-userData--:%@",dic); NSLog(@"error:%@",error); } //get dashboardData [self makeDashBoardData]; }]; [dataTask resume];

    //代理方法

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
        
        /*方法一 信任所有证书*/
        if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            if(completionHandler)
                completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }

     参考:

    iOS HTTPS证书不受信任解决办法

  • 相关阅读:
    python分包写入文件,写入固定字节内容,当包达到指定大小时继续写入新文件
    java 封装及this 用法
    [效率提升] 记一次使用工具编辑正则表达式快速输出匹配结果
    java用星星符号打印出一个直角三角形
    java按行和列进行输出数据
    java 三种循环及注意事项
    数据的运算,求和,两数求最大,三数求最大,两数是否相等
    采用位异或方式将两个变量数值调换
    今天遇到一件开心事,在eclipse编写的代码在命令窗口中编译后无法运行,提示 “错误: 找不到或无法加载主类”
    定义 java 基本数据类型
  • 原文地址:https://www.cnblogs.com/lulushen/p/9629449.html
Copyright © 2011-2022 走看看