Java访问SSL地址,免验证证书方式

 1 package com.lenovo.biportal.utils.bitool;
 2 
 3 import javax.net.ssl.HostnameVerifier;
 4 import javax.net.ssl.HttpsURLConnection;
 5 import javax.net.ssl.SSLContext;
 6 import javax.net.ssl.X509TrustManager;
 7 import java.io.BufferedReader;
 8 import java.io.InputStream;
 9 import java.io.InputStreamReader;
10 import java.net.HttpURLConnection;
11 
12 public class SSL {
13     final static HostnameVerifier DO_NOT_VERIFY = (hostname, session) -> true;
14 
15     public static void httpGet(String path) {
16         StringBuffer tempStr = new StringBuffer();
17         String responseContent = "";
18         HttpURLConnection conn = null;
19         try {
20             // Create a trust manager that does not validate certificate chains
21             trustAllHosts();
22             URL url = new URL(path);
23             HttpsURLConnection https = (HttpsURLConnection) url.openConnection();
24             if (url.getProtocol().toLowerCase().equals("https")) {
25                 https.setHostnameVerifier(DO_NOT_VERIFY);
26                 conn = https;
27             } else {
28                 conn = (HttpURLConnection) url.openConnection();
29             }
30             conn.connect();
31             System.out.println("地址:" + path + ", success, result: " + conn.getResponseCode() + " " + conn.getResponseMessage());
32             // HttpURLConnection conn = (HttpURLConnection)
33             // url.openConnection();
34 
35             // conn.setConnectTimeout(5000);
36             // conn.setReadTimeout(5000);
37             // conn.setDoOutput(true);
38 
39             InputStream in = conn.getInputStream();
40             conn.setReadTimeout(10 * 1000);
41             BufferedReader rd = new BufferedReader(new InputStreamReader(in,
42                     "UTF-8"));
43             String tempLine;
44             while ((tempLine = rd.readLine()) != null) {
45                 tempStr.append(tempLine);
46             }
47             responseContent = tempStr.toString();
48             System.out.println(responseContent);
49             rd.close();
50             in.close();
51         } catch (Exception e) {
52             logger.error("地址:{}, ins error", e);
53         } finally {
54             if (conn != null) {
55                 conn.disconnect();
56             }
57         }
58     }
59 
60     /**
61      * Trust every server - dont check for any certificate
62      */
63     public static void trustAllHosts() {
64 
65         // Create a trust manager that does not validate certificate chains
66         TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
67 
68             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
69                 return new java.security.cert.X509Certificate[]{};
70             }
71 
72             public void checkClientTrusted(X509Certificate[] chain, String authType) {
73 
74             }
75 
76             public void checkServerTrusted(X509Certificate[] chain, String authType) {
77 
78             }
79         }};
80 
81         // Install the all-trusting trust manager
82         // 忽略HTTPS请求的SSL证书,必须在openConnection之前调用
83         try {
84             SSLContext sc = SSLContext.getInstance("TLS");
85             sc.init(null, trustAllCerts, new java.security.SecureRandom());
86             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
87         } catch (Exception e) {
88             System.out.println("trustAllHosts is error" + e);
89         }
90     }
91 
92 }

 

转载:

https://ningyu1.github.io/site/post/53-ssl-cert-3/

posted @ 2021-03-25 15:24  曹丽是我女朋友。  阅读(226)  评论(0编辑  收藏  举报