JDK8使用HttpsURLConnection遇到的问题

2020-04-16  本文已影响0人  1994_老叶

最近在老代码中需要发送https请求,无法引入额外的jar包,只能使用java自带的类实现,下面是实现的相关代码
首先引入的包:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.*;

接下来是实现需要的类,我声明的内部类

private static class TrustAnyTrustManager implements X509TrustManager {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

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

        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[] {};
        }
    }

    private static class TrustAnyHostnameVerifier implements HostnameVerifier {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }
    }

接下来是方法的实现,get请求的实现:

public static String httpsSendGet(String url, int timeout, String encoding) throws Exception {
        String result = "";
        HttpsURLConnection conn = null;
        BufferedReader in = null;
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());
            URL console = new URL(url);
            conn = (HttpsURLConnection) console.openConnection();
            conn.setRequestMethod("GET");
            conn.setReadTimeout(timeout);
            conn.setConnectTimeout(timeout);
            conn.setSSLSocketFactory(sc.getSocketFactory());
            conn.setHostnameVerifier(new TrustAnyHostnameVerifier());
            conn.connect();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                in = new BufferedReader(new InputStreamReader(conn
                        .getInputStream(), encoding));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                return result;
            } else {
            }
        } catch (Exception e) {
            throw e;
        } finally {
            if (in != null) {
                in.close();
            }
            if (conn != null) {
                try {
                    conn.getInputStream().close();
                } catch (Throwable e) {

                }
                try {
                    conn.getOutputStream().close();
                } catch (Throwable e) {

                }
                conn.disconnect();
            }

        }
        return result;
    }

但是在测试调用的时候,总是报错:java.net.SocketException: Connection reset,查了半天的问题,也是没有解决,发现问题上的原因是,在两台服务器上测试,一台总是可以,一台总是不行,两台的服务器的区别在于jdk,前者使用jdk1.8.0_181,后者使用jdk1.8.0_77,我将不行的那一台服务器的jdk更换为jdk1.8.0_181便不会报错,没想到是jdk版本的原因,先记录于此,具体差异需要研究一下二者有何不同。
待续

上一篇 下一篇

猜你喜欢

热点阅读