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

  • 相关阅读:
    java OA系统 自定义表单 流程审批 电子印章 手写文字识别 电子签名 即时通讯
    flowable 获取当前任务流程图片的输入流
    最新 接口api插件 Swagger3 更新配置详解
    springboot 集成 activiti 流程引擎
    java 在线考试系统源码 springboot 在线教育 视频直播功能 支持手机端
    阿里 Nacos 注册中心 配置启动说明
    springboot 集成外部tomcat war包部署方式
    java 监听 redis 过期事件
    springcloudalibaba 组件版本关系
    java WebSocket 即时通讯配置使用说明
  • 原文地址:https://www.cnblogs.com/lulushen/p/9640150.html
Copyright © 2011-2022 走看看