Https遇到的问题,证书转换失败在[pinnedCertifi

2017-04-19  本文已影响0人  iOS乐乐

1.最近在实现Https的请求,但是在项目导入cer证书后,直接在Afnetworking中断点崩溃在这里,查找了好多的原因终于找到了问题的所在, 其实是.cer证书转换的时候出现了问题!
2.我们可以找后台要一下.crt证书,
3.我就用这个指令转换的:openssl x509 -in pn.crt -out ccc.cer -outform der,重新生成一个.cer证书这样就可以了😀😀
4.希望这个简书对遇到同样问题的朋友有所帮助😀😀😀

[pinnedCertificates addObject:(__bridge_transfer id)SecCertificateCreateWithData(NULL, (__bridge CFDataRef)certificateData)]

首先先实现Https的配置

    //先导入证书,找到证书的路径
    NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"newll" ofType:@"cer"];
    NSData *certData = [NSData dataWithContentsOfFile:cerPath];
    
    NSSet * certSet = [[NSSet alloc] initWithObjects:certData, nil];
    AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    // 是否允许,NO-- 不允许无效的证书
    [securityPolicy setAllowInvalidCertificates:YES];
    // 设置证书
    [securityPolicy setPinnedCertificates:certSet];
     securityPolicy.validatesDomainName = NO;
       
    AFHTTPSessionManager * manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes=[NSSet setWithObjects:@"application/json", @"text/html",@"text/json", @"text/javascript",@"text/plain", nil];
    //关闭缓存避免干扰测试
    manager.requestSerializer.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
    manager.securityPolicy=securityPolicy;
    NSString*urlStr=[NSString stringWithFormat:@"%@/userLogin",@"https://192.168.150.239/pnservice"];
    
 NSDictionary*params=@{@"userName":@"18132097435",@"userPassword":@"123456",@"appA":[NSString stringWithFormat:@"%d",a]};
    
    [HTTPTools POST:urlStr parameters:params success:^(id responseObject) {
        if (responseObject)
        {

            NSLog(@"%@",responseObject);
            
            NSString* numberStr=[NSString stringWithFormat:@"%@",responseObject[@"resultCode"]];
            if ([numberStr isEqualToString:@"1"])
            {
            }
            
        }
    } failure:^(NSError *error) {
        if (error)
        {
            NSLog(@"%@",error);

        }
    }];

上一篇下一篇

猜你喜欢

热点阅读