本文主要是介绍AFNetworking2的SSL身份认证 -1022错误解决办法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
错误说明,AFNetworking发送Https的网络请求时,往往会遇到-1012错误
Error Domain=NSURLErrorDomain Code=-1012 "The operation couldn’t be completed. (NSURLErrorDomain error -1012.)" UserInfo=0x8d635f0 {NSErrorFailingURLKey=...URL...}
错误解决办法:
1、下载需要访问地址的cer证书或者别的格式转换出cer证书,放入mainbundle中
转换方法 openssl x509 -in 你的证书.crt -out 你的证书.cer -outform der
2、编写以下方法设置AFHTTPRequestOperationManager的securityPolicy属性,特别注意红字的两行代码
- (AFSecurityPolicy*)customSecurityPolicy
{
/**** SSL Pinning ****/
NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"证书名" ofType:@"cer"];
NSData *certData = [NSData dataWithContentsOfFile:cerPath];
AFSecurityPolicy *securityPolicy = [[AFSecurityPolicy alloc] init];
[securityPolicy setAllowInvalidCertificates:YES];
[securityPolicy setValidatesCertificateChain:NO];
[securityPolicy setPinnedCertificates:@[certData]];
[securityPolicy setSSLPinningMode:AFSSLPinningModeCertificate];
/**** SSL Pinning ****/
return securityPolicy;
}
2 设置manager
AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
[mgr setSecurityPolicy:[self customSecurityPolicy]];
3发送请求:
NSDictionary *params = @{ 参数 };
[mgr GET:"URL字符串" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
成功代码
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
失败代码
}];
这篇关于AFNetworking2的SSL身份认证 -1022错误解决办法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!