HttpsURLConnection

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpsURLConnection相关的知识,希望对你有一定的参考价值。

package test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.KeyManager;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

public class Test {
    public static void main(String[] args) {
        HttpsURLConnection conn = null;
        try {
            URL url = new URL("https://home.cnblogs.com/u/mjn0");
            conn = (HttpsURLConnection) url.openConnection();
            SSLContext context = SSLContext.getInstance("TLS");

            X509TrustManager xtm = new X509TrustManager() {
                @Override
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

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

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

            KeyManager[] km = null;
            TrustManager[] tm = new TrustManager[] { xtm };
            SecureRandom random = null;

            context.init(km, tm, random);
            SSLSocketFactory sf = context.getSocketFactory();
            conn.setSSLSocketFactory(sf);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }

        try (InputStream is = conn.getInputStream();
                InputStreamReader isr = new InputStreamReader(is, "utf-8");
                BufferedReader br = new BufferedReader(isr);) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

以上是关于HttpsURLConnection的主要内容,如果未能解决你的问题,请参考以下文章

HttpsURLConnection 身份验证器错误

如何通过 HttpsURLConnection 使用证书身份验证?

Android:使用 HttpsURLConnection 的 HTTPS (SSL) 连接

Java 1.6 HttpsURLConnection:java.net.SocketException:连接重置

使用weblogic作为容器遇到的问题---URL.openConnection获取连接返回HttpsURLConnection与SOAPHttpsURLConnection

HttpsURLConnection 忽略 setSSLSocketFactory