微信官方接口之获取证书以及使用

如何根据链接获取证书?

private static final String keyPassword = "这里填写的证书密码"//微信官方下载的证书密码一般是商户号
private static SSLContext wx_ssl_context = null;
try {
            FileInputStream inputStream = new FileInputStream("这里填写你的证书的绝对路径");
            KeyStore keystore = KeyStore.getInstance("PKCS12");//p12格式证书
            //证书密码
            char[] keyPassword = TransfersConfig.mch_id.toCharArray();//将密码装换为字符数组
            keystore.load(inputStream, keyPassword);//加载
            wx_ssl_context  = SSLContexts.custom().loadKeyMaterial(keystore, keyPassword).build();//获取到的证书
        } catch (Exception e) {
            e.printStackTrace();
        }

如何在调用接口的时候使用证书?

private static SSLConnectionSocketFactory getSSLConnectionSocket() {
              //wx_ssl_context是上一步获取到的证书
        return new SSLConnectionSocketFactory(wx_ssl_context, new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}, null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    }

private static CloseableHttpClient buildHttpClien() {
              CloseableHttpClient httpClient = HttpClients.custom()
                    .setSSLSocketFactory(getSSLConnectionSocket()).build();
              return httpClient;//提醒一下记得关闭httpClient,httpClient.close();
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容