Java发送HTTPS请求

public static String httpsGet(String urlStr) {
        URL url;

        try {
            url = new URL(urlStr);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

            X509TrustManager xtm = new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    // TODO Auto-generated method stub
                    return null;
                }

                @Override
                public void checkServerTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }

                @Override
                public void checkClientTrusted(X509Certificate[] arg0, String arg1)
                        throws CertificateException {
                    // TODO Auto-generated method stub

                }
            };
            TrustManager[] tm = {xtm};

            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tm, null);

            con.setSSLSocketFactory(ctx.getSocketFactory());

            con.setHostnameVerifier((arg0, arg1) -> true);

            BufferedReader read = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String temp;
            StringBuilder re = new StringBuilder();
            while ((temp = read.readLine()) != null) {
                re.append(temp);
            }
            return re.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

你可能感兴趣的:(Java发送HTTPS请求)