zoukankan      html  css  js  c++  java
  • 使用WeexSDK,网络请求信任证书的问题

    使用0.18.0版本的weexSDK,并且是手动导入的SDK。

    在项目中创建一个SDResourceRequest(名字随意)类继承WXResourceRequestHandlerDefaultImpl,遵循

    WXResourceRequestHandler,NSURLSessionDelegate协议,如下图:

    在SDResourceRequest.m中实现- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler方法,代码如下:

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler {
     
        //AFNetworking中的处理方式
        NSURLSessionAuthChallengeDisposition disposition = NSURLSessionAuthChallengePerformDefaultHandling;
        __block NSURLCredential *credential = nil;
        //判断服务器返回的证书是否是服务器信任的
        if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
            credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            /*disposition:如何处理证书
             NSURLSessionAuthChallengePerformDefaultHandling:默认方式处理
             NSURLSessionAuthChallengeUseCredential:使用指定的证书   
          NSURLSessionAuthChallengeCancelAuthenticationChallenge:取消请求 */ if (credential) { disposition = NSURLSessionAuthChallengeUseCredential; } else { disposition = NSURLSessionAuthChallengePerformDefaultHandling; } } else { disposition = NSURLSessionAuthChallengeCancelAuthenticationChallenge; } //安装证书 if (completionHandler) { completionHandler(disposition, credential); } }

    使用下面的方法无效,不知道为什么

    - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler{
        
        /*方法一 信任所有证书*/
        if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            if(completionHandler)
                completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }

    使用cocoaPods导入WeexSDK应该没有问题,但是手动导入的WeexSDK可能没有WXResourceRequestHandlerDefaultImpl.h,可以在git上下载weexSDK源码把WXResourceRequestHandlerDefaultImpl.h放在外面,重新打包后在导入项目,如图:

    参考:

    AFNetworking中的处理方式的参考链接找不到了

    https://www.jianshu.com/p/bcb19fe43909

    https://www.jianshu.com/p/3cc2ec005761

  • 相关阅读:
    PCI Express体系结构导读(3)- PCIE
    PCI Express体系结构导读(2)
    npm添加代理和取消代理
    流媒体压力测试rtmp&hls(含推流和拉流)
    流媒体跳坑: 03-视频会议:使用LiveRTC来做视频直播
    流媒体跳坑: 02-视频会议:Webrtc服务器
    正交调制解调(MATLAB)
    mp4文件断电保存-(关于:MP4视频文件损坏的一点想法)
    流媒体跳坑: 01-Mp4 文件解析
    centos 守护 node 进程
  • 原文地址:https://www.cnblogs.com/lulushen/p/9640150.html
Copyright © 2011-2022 走看看