关于WxJava微信开发如何绕过证书验证问题
- 默认使用的是WxCpServiceImpl,使用apache httpclient实现网络请求 *

首先新建一个类
我们进入刚刚的WxCpServiceImpl父类,WxCpServiceApacheHttpClientImpl
可以看到:

在我们新建的类中去继承BaseWxCpServiceImpl<CloseableHttpClient, HttpHost>
然后把WxCpServiceApacheHttpClientImpl 中代码全部复制一遍
修改其中的方法

插入这些

@Override
public void initHttp() {
ApacheHttpClientBuilder apacheHttpClientBuilder = this.configStorage
.getApacheHttpClientBuilder();
if (null == apacheHttpClientBuilder) {
apacheHttpClientBuilder = DefaultApacheHttpClientBuilder.get();
}
SSLConnectionSocketFactory sslsf = null;
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
sslsf = new SSLConnectionSocketFactory(
sslContext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
} catch (Exception e) {
log.error("微信ssl忽略异常");
}
apacheHttpClientBuilder.httpProxyHost(this.configStorage.getHttpProxyHost())
.httpProxyPort(this.configStorage.getHttpProxyPort())
.httpProxyUsername(this.configStorage.getHttpProxyUsername())
.httpProxyPassword(this.configStorage.getHttpProxyPassword());
if (sslsf != null) {
apacheHttpClientBuilder.sslConnectionSocketFactory(sslsf);
}
if (this.configStorage.getHttpProxyHost() != null && this.configStorage.getHttpProxyPort() > 0) {
this.httpProxy = new HttpHost(this.configStorage.getHttpProxyHost(), this.configStorage.getHttpProxyPort());
}
this.httpClient = apacheHttpClientBuilder.build();
}
这样 下次直接使用 我们自定义的service去调用微信方法
本文介绍了一种在WxJava微信开发中绕过证书验证的方法,通过自定义WxCpServiceImpl,实现ApacheHttpClient的网络请求,信任所有SSL证书,避免了因证书问题导致的开发障碍。


被折叠的 条评论
为什么被折叠?



