java https请求提示protocol is disabled or cipher suites are inappropriate

1,作者遇见的是HttpClients请求,使用p12证书。windows请求正常,放到linux上就报javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)这个错误,原因是指示SSL/TLS握手过程中未能找到合适的协议版本或密码套件。这通常是因为服务器和客户端之间支持的加密协议或密码套件不匹配。

2,在linux上使用下面这条命令会列出所有支持的密码套件及其支持的协议版本,你可以从中查找是否有TLSv1相关的条目。

openssl ciphers -v | grep "TLSv1"

查询结果:

3,修改代码或者需要安装TLSv1版本。作者选择修改代码,修改代码如下:

 public static String refund(WXRequestRefundEntity refund, String url, String SSLCERT_PATH, String SSLCERT_PASSWORD) throws Exception {
        if (StringUtils.isBlank(refund.getKey())) {
            throw new Exception("key不能为空");
        }
        String data = getXml(refund);
        CloseableHttpClient httpclient = null;
        CloseableHttpResponse response = null;
        try {
            KeyStore keyStore = KeyStore.getInstance("PKCS12");
            FileInputStream instream = new FileInputStream(new File(SSLCERT_PATH));//P12

            keyStore.load(instream, SSLCERT_PASSWORD.toCharArray());
            instream.close();

            SSLContext sslcontext = SSLContexts.custom()
                    .loadKeyMaterial(keyStore, SSLCERT_PASSWORD.toCharArray())
                    .build();

            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    sslcontext,
                    new String[]{"TLSv1.2"},  //把这里改成对应系统里面的版本即可
                    null,
                    SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);

            httpclient = HttpClients.custom()
                    .setSSLSocketFactory(sslsf)
                    .build();

            HttpPost httpost = new HttpPost(url); 
            httpost.addHeader("Connection", "keep-alive");
            httpost.addHeader("Accept", "*/*");
            httpost.addHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            httpost.addHeader("Host", "api.mch.weixin.qq.com");
            httpost.addHeader("X-Requested-With", "XMLHttpRequest");
            httpost.addHeader("Cache-Control", "max-age=0");
            httpost.addHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0) ");
            httpost.setEntity(new StringEntity(data, "UTF-8"));
            response = httpclient.execute(httpost);

            return EntityUtils.toString(response.getEntity(), "UTF-8");
        }finally {
            if (httpclient != null){
                httpclient.close();
            }
            if (response != null){
                response.close();
            }
        }
    }

请作者喝杯咖啡

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值