HttpsConnection访问https

  1.  public static void main(String[] args) {  
  2.         // Create a trust manager that does not validate certificate chains  
  3.         TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {  
  4.             public void checkClientTrusted(  
  5.                     java.security.cert.X509Certificate[] arg0, String arg1)  
  6.                     throws CertificateException {  
  7.             }  
  8.   
  9.             public void checkServerTrusted(  
  10.                     java.security.cert.X509Certificate[] arg0, String arg1)  
  11.                     throws CertificateException {  
  12.             }  
  13.   
  14.             public java.security.cert.X509Certificate[] getAcceptedIssuers() {  
  15.                 return new X509Certificate[0];  
  16.             }  
  17.         } };  
  18.           
  19.         // Ignore differences between given hostname and certificate hostname  
  20.         HostnameVerifier hv = new HostnameVerifier() {  
  21.             public boolean verify(String hostname, SSLSession session) {  
  22.                 return true;  
  23.             }  
  24.         };  
  25.   
  26.         // Install the all-trusting trust manager  
  27.         try {  
  28.             SSLContext sc = SSLContext.getInstance("SSL");  
  29.             sc.init(null, trustAllCerts, new SecureRandom());  
  30.             HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());  
  31.             HttpsURLConnection.setDefaultHostnameVerifier(hv);  
  32.               
  33.             URL url = new URL("https://127.0.0.1");  
  34.             URLConnection con = url.openConnection();  
  35.             BufferedReader in = new BufferedReader(new InputStreamReader(  
  36.                     con.getInputStream()));  
  37.             String x = in.readLine();  
  38.             System.out.println(x);  
  39.             in.close();  
  40.         } catch (Exception e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.     } 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值