NSURLSession/NSURLConnection HTTP load
failed (kCFStreamErrorDomainSSL, -9843)
要说一下的是 ,我的plist文件中添加了NSAppTransportSecurity类型Dictionary。
在NSAppTransportSecurity下添加NSAllowsArbitraryLoads类型Boolean,值设为YES
出现这种问题, 是因为 https信任的证书出了问题
原因是苹果的 官方资料说首先必须要基于TLS
1.2版本协议。然后证书的加密的算法还需要达到SHA256或者更高位的RSA密钥或ECC密钥,如果不符合,请求将被中断并返回nil.
详情:https://developer.apple.com/library/prerelease/ios/technotes/App-Transport-Security-Technote/
因为信任证书出了问题,导致了解析的失败
客户端信任证书的过程:
1.当客户端要访问服务器的时候,服务器向客户端发送受保护的信任证书
2.客户端判断是否对客户端发送的证书进行信任,,
3.如果信任.则客户端会安装公钥在客户端,而服务器就拥有受保护证书的密钥,每一次向服务器请求数据的时候,服务器会先将要发送的数据进行密钥加密,客户端对所传数据通过公钥解密
因为我用的是af,所以第三方中 有跳过验证证书的方法。 af 中有个securityPolicy
安全策略,在这里 可以跳过。
当然,如果是更早的版本的 af
中 有一个allowsInvalidSSLCertificate
的方法
在原生中
有NSURLSession和NSURLConnection两种信任证书的方法
-(void)URLSession:(NSURLSession
*)session
task:(NSURLSessionTask
*)task
didReceiveChallenge:(NSURLAuthenticationChallenge
*)challenge
completionHandler:(void
(^)(NSURLSessionAuthChallengeDisposition
disposition,
NSURLCredential
*
__nullable
credential))completionHandler
{
// 判断是否是信任服务器证书
if(challenge.protectionSpace.authenticationMethod
==
NSURLAuthenticationMethodServerTrust)
{
// 告诉服务器,客户端信任证书
//
创建凭据对象
NSURLCredential
*credntial
= [NSURLCredential
credentialForTrust:challenge.protectionSpace.serverTrust];
//
通过completionHandler告诉服务器信任证书
completionHandler(NSURLSessionAuthChallengeUseCredential,credntial);
}
NSLog(@"protectionSpace
= %@",challenge.protectionSpace);
}
-(void)connection:(NSURLConnection
*)connection
willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge
*)challenge
{
// 判断是否是信任服务器证书
if(challenge.protectionSpace.authenticationMethod
==
NSURLAuthenticationMethodServerTrust)
{
// 告诉服务器,客户端信任证书
//
创建凭据对象
NSURLCredential
*credntial
= [NSURLCredential
credentialForTrust:challenge.protectionSpace.serverTrust];
//
告诉服务器信任证书
[challenge.sender
useCredential:credntial
forAuthenticationChallenge:challenge];
}
}