关于WxJava微信开发如何绕过证书验证问题

关于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去调用微信方法

发布了9 篇原创文章 · 获赞 1 · 访问量 2491

猜你喜欢

转载自blog.csdn.net/tgb7895/article/details/100725260