https 请求:https load failed -1202
问题描述:
使用原生接口进行网络请求的工程,https请求返回error:https load failed -1202 info.plist中也配置了允许访问非安全性http的声明
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
但是,用NSURLSession访问https无法返回数据(服务端正常)
问题原因:
终端没有自动处理证书
问题解决:
使用NSURLSession代理方法,在服务器返回证书信息的时候,处理证书(使用/忽略/拒绝)
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:queue];
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler//通过调用block,来告诉NSURLSession要不要收到这个证书
{
//(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler
//NSURLSessionAuthChallengeDisposition (枚举)如何处理这个证书
//NSURLCredential 授权
//认证证书通过
if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){//服务器信任证书
NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];//服务器信任证书
if(completionHandler)
completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
}
NSLog(@"....completionHandler---:%@",challenge.protectionSpace.authenticationMethod);
}
可以使用基于AFNetworking的网络框架,非SDK程序避免使用原生!
*切记不要再使用老的http请求接口(IOS9废弃):[NSURLConnection sendAsynchronousRequest:... *