- public static void main(String[] args) {
- // Create a trust manager that does not validate certificate chains
- TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
- public void checkClientTrusted(
- java.security.cert.X509Certificate[] arg0, String arg1)
- throws CertificateException {
- }
- public void checkServerTrusted(
- java.security.cert.X509Certificate[] arg0, String arg1)
- throws CertificateException {
- }
- public java.security.cert.X509Certificate[] getAcceptedIssuers() {
- return new X509Certificate[0];
- }
- } };
- // Ignore differences between given hostname and certificate hostname
- HostnameVerifier hv = new HostnameVerifier() {
- public boolean verify(String hostname, SSLSession session) {
- return true;
- }
- };
- // Install the all-trusting trust manager
- try {
- SSLContext sc = SSLContext.getInstance("SSL");
- sc.init(null, trustAllCerts, new SecureRandom());
- HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
- HttpsURLConnection.setDefaultHostnameVerifier(hv);
- URL url = new URL("https://127.0.0.1");
- URLConnection con = url.openConnection();
- BufferedReader in = new BufferedReader(new InputStreamReader(
- con.getInputStream()));
- String x = in.readLine();
- System.out.println(x);
- in.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
HttpsConnection访问https
最新推荐文章于 2024-02-03 09:20:52 发布