iOS https请求第一步从服务器下载证书
2016-10-17 09:26
375 查看
+ (void)InitCertWithBlock:(void (^)(BOOL))block { NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject]; NSLog(@"--->%@", docPath); NSString *certDirPath = [docPath stringByAppendingPathComponent:@"Cert"]; NSString *certTxtPath = [certDirPath stringByAppendingPathComponent:@"CertTxt.txt"]; NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager createDirectoryAtPath:certDirPath withIntermediateDirectories:YES attributes:nil error:nil]; NSURLSession *session = [NSURLSession sharedSession]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:DWGETCER]]; request.HTTPMethod = @"POST"; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!error) { NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; if ([dic[@"errorCode"] intValue] == 0) { NSString *certFileURL = dic[@"data"]; if ([fileManager fileExistsAtPath:certTxtPath] && [fileManager fileExistsAtPath:[certDirPath stringByAppendingPathComponent:@"bemm.cer"]]) { NSString *localCertFileURL = [NSString stringWithContentsOfFile:certTxtPath encoding:NSUTF8StringEncoding error:nil]; if ([certFileURL isEqualToString:localCertFileURL]) { block(YES); return ; } } //下载证书 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:certFileURL]]; NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { if (!error) { [data writeToFile:[certDirPath stringByAppendingPathComponent:@"bemm.cer"] atomically:YES]; [certFileURL writeToFile:certTxtPath atomically:YES encoding:NSUTF8StringEncoding error:nil]; block(YES); return ; } else { block(NO); return ; } }]; [task resume]; } else { block(NO); return ; } } else { block(NO); return; } }]; [task resume]; }
相关文章推荐
- iOS HTTPS安全请求 验证服务器返回的证书
- ios企业版发布 https证书以及服务器设置
- iOS开发中AFNetworking中如何设置https请求(使用自建证书)
- iOS使用自签名证书实现HTTPS请求
- iOS开发:对于AFNetworking HTTP转HTTPS请求证书问题
- iOS之Https自签名证书认证及数据请求的封装原理
- iOS开发 https请求网络web自定义证书不信任解决方案
- iOS 用自签名证书实现 HTTPS 请求的原理
- iOS使用自签名证书实现HTTPS请求
- iOS 用自签名证书实现 HTTPS 请求的原理实例讲解
- iOS基于AFNetworking使用自签名证书实现HTTPS请求
- iOS https请求对自签名证书忽略
- iOS客户端校验https网络请求证书
- [置顶] iOS使用自签名证书实现HTTPS请求
- iOS https请求 忽略证书验证
- HttpWebRequest加载证书请求远端https服务器时 : 基础连接已经关闭: 无法与远程服务器建立信任关系
- iOS使用自签名证书实现HTTPS请求
- iOS使用自签名证书实现HTTPS请求
- 微信小程序 HTTPS 请求,如何获取免费证书配置服务器
- iOS, 使用自签名证书https请求,(NSURLSession)