NSURLSession https load failed -1202


Edit

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:... *

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值