java post java.net.SocketException: Connection reset

js&jquery

在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

如果你觉得本文对你有益,并且你愿意,欢迎赞助一点辛苦费给我,嘻嘻!
支付宝:支付宝微信钱包:微信钱包
悠乐小呆

博主:悠乐小呆

擅长:web开发

技术:html css js jquery asp.net

略懂:php android

喃语:人生不设限|勿畏首畏尾|超强执行力

热门评论