在https post请求时发生这个错误

原因:使用的jdk1.7,只支持TLSv1,不支持TLSv1.1 和 TLSv1.2

使用工具类处理:

方法一:

public static String postTLS(String requestUrl, Map<String, String> params)  {
    HttpPost httpPost = new HttpPost(requestUrl);
    CloseableHttpClient createSSLClientDefault = createSSLClientDefault("");
    CloseableHttpResponse httpResponse = null;
    try {

        logger.info("request httpspost:" + requestUrl);
        List<NameValuePair> list = new ArrayList();
        for (String key: params.keySet()) {
            list.add(new BasicNameValuePair(key, params.get(key)));
        }
        StringEntity stringEntity = new UrlEncodedFormEntity(list, "UTF-8");
        stringEntity.setContentType("application/x-www-form-urlencoded");
        httpPost.setEntity(stringEntity);

        logger.info("execute post...");
        httpResponse = createSSLClientDefault.execute(httpPost);
        StatusLine statusLine = httpResponse.getStatusLine();
        int state = statusLine.getStatusCode();
        logger.info("response status: " + state);
        if (state == HttpStatus.SC_OK) {
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity,"UTF-8");
            logger.info("httpspost result:" + result);
            return result;
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (httpResponse != null) {
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            createSSLClientDefault.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

private static CloseableHttpClient createSSLClientDefault(String tls) {
    try {
        X509TrustManager trustManager = new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
            }

            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return null;
            }

        };

        SSLContext sslContext = SSLContext.getInstance("TLSv1.2");
        TrustManager[] tm = new TrustManager[]{trustManager};
        sslContext.init(null, tm, new java.security.SecureRandom());

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } catch (KeyManagementException e) {
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
    return HttpClients.createDefault();
}

方法二:使用Bouncy Castle

https://blog.csdn.net/qq_32016029/article/details/80222013


参考资料:https://www.v2ex.com/t/259455

最后修改:2019 年 11 月 14 日
如果觉得我的文章对你有用,请随意赞赏