zoukankan      html  css  js  c++  java
  • Java爬虫--Https绕过证书

      https网站服务器都是有证书的。 是由网站自己的服务器签发的,并不被浏览器或操作系统广泛接受。

      在使用CloseableHttpClient时经常遇到证书错误(知乎的网站就是这样)

      现在需要SSL绕过证书,下面直接贴出代码,调用时只需要在发送请求后  new  HttpsBerBer(文件的字节码) ; 

     1 import javax.net.ssl.HostnameVerifier;
     2 import javax.net.ssl.HttpsURLConnection;
     3 import javax.net.ssl.SSLSession;
     4 import java.util.logging.Logger;
     5 
     6 public class HttpsBerBer {
     7     public HttpsBerBer(String name) throws Exception {
     8         logger = Logger.getLogger(name);
     9         trustAllHttpsCertificates();
    10         HostnameVerifier hv = new HostnameVerifier() {
    11             public boolean verify(String urlHostName, SSLSession session) {
    12                 logger.info("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
    13                 return true;
    14             }
    15         };
    16         HttpsURLConnection.setDefaultHostnameVerifier(hv);
    17     }
    18     private static Logger logger ;
    19     private static void trustAllHttpsCertificates() throws Exception {
    20         javax.net.ssl.TrustManager[] trustAllCerts = new javax.net.ssl.TrustManager[1];
    21         javax.net.ssl.TrustManager tm = new miTM();
    22         trustAllCerts[0] = tm;
    23         javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext
    24                 .getInstance("SSL");
    25         sc.init(null, trustAllCerts, null);
    26         javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc
    27                 .getSocketFactory());
    28     }
    29     static class miTM implements javax.net.ssl.TrustManager,
    30             javax.net.ssl.X509TrustManager {
    31         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
    32             return null;
    33         }
    34 
    35         public boolean isServerTrusted(
    36                 java.security.cert.X509Certificate[] certs) {
    37             return true;
    38         }
    39 
    40         public boolean isClientTrusted(
    41                 java.security.cert.X509Certificate[] certs) {
    42             return true;
    43         }
    44 
    45         public void checkServerTrusted(
    46                 java.security.cert.X509Certificate[] certs, String authType)
    47                 throws java.security.cert.CertificateException {
    48             return;
    49         }
    50 
    51         public void checkClientTrusted(
    52                 java.security.cert.X509Certificate[] certs, String authType)
    53                 throws java.security.cert.CertificateException {
    54             return;
    55         }
    56     }
    57 }

      

         

  • 相关阅读:
    CentOS Linux使用crontab运行定时任务详解
    pycharm自定义代码片段
    迭代器:斐波那契数列
    mysql复习-来源考试
    复杂和遗留的数据库schema
    ubuntu系统AndroidStudio修改内存大小
    Hibernate设置时间戳的默认值和更新时间的自动更新
    Android 自定义view --圆形百分比(进度条)
    MYSQL 更新时间自动同步与创建时间默认值共存问题
    ubuntu virtualbox xp无声音解决
  • 原文地址:https://www.cnblogs.com/LexMoon/p/JavaBerBer.html
Copyright © 2011-2022 走看看