zoukankan      html  css  js  c++  java
  • HttpURLConnection绕过HTTPS的SSL验证

    (这个jdk环境需要是1.8,以上)。

    直接在类里面加一个static代码块

     1 static {
     2         try {
     3             trustAllHttpsCertificates();
     4             HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
     5                 public boolean verify(String urlHostName, SSLSession session) {
     6                     return true;
     7                 }
     8             });
     9         } catch (Exception e) {
    10         }
    11     }

      在调用如下方法:

     1 private static void trustAllHttpsCertificates() throws NoSuchAlgorithmException, KeyManagementException {
     2         TrustManager[] trustAllCerts = new TrustManager[1];
     3         trustAllCerts[0] = new TrustAllManager();
     4         SSLContext sc = SSLContext.getInstance("SSL");
     5         sc.init(null, trustAllCerts, null);
     6         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
     7     }
     8 
     9     private static class TrustAllManager implements X509TrustManager {
    10         public X509Certificate[] getAcceptedIssuers() {
    11             return null;
    12         }
    13 
    14         public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
    15         }
    16 
    17         public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
    18         }
    19     }

    这样就应该没有问题了。

  • 相关阅读:
    老罗的OLLYMACHINE
    VGA寄存器一览表
    常用的I/O地址
    使用VESA示例
    打开A20
    Linux 2.2 Framebuffer Device Programming Tutorial
    Linux驱动
    基于Linux核心的汉字显示的尝试
    汉字的动态编码与显示方案
    AT&T语法(一)
  • 原文地址:https://www.cnblogs.com/jylee/p/9391876.html
Copyright © 2011-2022 走看看