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

  • 相关阅读:
    python django 创建app失败 升级sqlite3 或者修改数据库连接为mysql
    Decima json 化
    openpyxl excel操作
    centos添加python虚拟环境
    oracle的参数文件:pfile和spfile
    修改 db_unique_name
    工具 --- Vim
    部署 --- Nginx
    工具 --- Git使用
    django 之(五) --- RESTApi总结
  • 原文地址:https://www.cnblogs.com/lulushen/p/9640150.html
Copyright © 2011-2022 走看看