weex https 设置
程序员文章站
2024-03-24 18:28:34
...
在android上:
扩展一个Adapter,继承DefaultWXHttpAdapter
-
public class BingoWXHttpAdapter extends DefaultWXHttpAdapter {
@Override protected HttpURLConnection createConnection(URL url) throws IOException { HttpURLConnection conn = null; if (url.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); HttpsURLConnection httpsCon = (HttpsURLConnection) url.openConnection(); httpsCon.setHostnameVerifier(DO_NOT_VERIFY); conn = httpsCon; } else { conn = (HttpURLConnection) url.openConnection(); } return conn; } //host不验证 private HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; //信任所有证书 private static void trustAllHosts() { final String TAG = "trustAllHosts"; TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return new java.security.cert.X509Certificate[]{}; } public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { Log.i(TAG, "checkClientTrusted"); } public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { Log.i(TAG, "checkServerTrusted"); } }}; try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } catch (Exception e) { e.printStackTrace(); } } }
之后在初始化Engine的时候注册它:
setHttpAdapter(new BingoWXHttpAdapter())
在iOS上
继承WXResourceRequestHandlerDefaultImpl实现didReceiveChallenge,具体代码如下:
#import <Foundation/Foundation.h>
#import "WXResourceRequestHandlerDefaultImpl.h"
#import "WXResourceRequestHandler.h"
@interface BingoWXNetworkImpl : WXResourceRequestHandlerDefaultImpl <WXResourceRequestHandler,NSURLSessionDataDelegate>
@end
#import "BingoWXNetworkImpl.h"
@implementation BingoWXNetworkImpl
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * __nullable credential))completionHandler {
// NSURLSessionAuthChallengeUseCredential = 0, 使用(信任)证书
// NSURLSessionAuthChallengePerformDefaultHandling = 1, 默认,忽略
// NSURLSessionAuthChallengeCancelAuthenticationChallenge = 2, 取消
// NSURLSessionAuthChallengeRejectProtectionSpace = 3, 这次取消,下载次还来问
NSLog(@"%@>>>>>>>>>>>>>>>>>>>>>>>>",challenge.protectionSpace);
// 如果是请求证书信任,我们再来处理,其他的不需要处理
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
NSURLCredential *cre = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
// 调用block
completionHandler(NSURLSessionAuthChallengeUseCredential,cre);
}
}
@end
更多详解:
喜欢可以加@群号:913934649
简书: https://www.jianshu.com/u/88db5f15770d
csdn:https://me.csdn.net/beyondforme
掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7
推荐阅读
-
weex https 设置
-
FE - weex 开发之 配置 vue 与 vue - router环境
-
tomcat 设置字符集编码 博客分类: javaWeb学习
-
wget代理设置 博客分类: linux Proxy Authentication Requirewget
-
tomcat 设置字符集编码 博客分类: javaWeb学习
-
(五)RecycleView 动态设置改变列表显示的高度,禁止滑动
-
7种垃圾收集器:主要特点 应用场景 设置参数 基本运行原理 博客分类: JVM调优
-
springboot部署到阿里云,配置https,springboot项目同时支持http和https请求,阿里云配置https
-
Springboot 同时支持Http和Https访问
-
【Spring】配置SpringBoot同时支持http和https访问