美文网首页
Jsoup不能访问HTTPS解决

Jsoup不能访问HTTPS解决

作者: Uning | 来源:发表于2020-03-07 11:48 被阅读0次
    import java.io.UnsupportedEncodingException;
    import java.security.SecureRandom;
    import java.security.cert.CertificateException;
    import java.security.cert.X509Certificate;
     
    import javax.net.ssl.HostnameVerifier;
    import javax.net.ssl.HttpsURLConnection;
    import javax.net.ssl.SSLContext;
    import javax.net.ssl.SSLSession;
    import javax.net.ssl.X509TrustManager;
    
        /**
         * 信任任何站点,实现https页面的正常访问
         * 
         */
        
        public static void trustEveryone() {
            try {  
                HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
                    public boolean verify(String hostname, SSLSession session) {
                        return true;  
                    }
                });  
      
                SSLContext context = SSLContext.getInstance("TLS");  
                context.init(null, new X509TrustManager[] { new X509TrustManager() {
                    public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
      
                    public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    }
      
                    public X509Certificate[] getAcceptedIssuers() {
                        return new X509Certificate[0];  
                    }
                } }, new SecureRandom());  
                HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
            } catch (Exception e) {
                // e.printStackTrace();  
            }
        }
    
    public static void main(String[] args) {
                trustEveryone();
                Document doc = Jsoup.connect("xxxx").get();
    }
    

    OK

    转载自
    原文链接:https://blog.csdn.net/shaochong047/article/details/79636142

    相关文章

      网友评论

          本文标题:Jsoup不能访问HTTPS解决

          本文链接:https://www.haomeiwen.com/subject/lzpqdhtx.html