java的https请求解决证书问题

Posted 豆苗稀

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java的https请求解决证书问题相关的知识,希望对你有一定的参考价值。

package sqr.srchSpider.utils;

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;

public class TrustSSL {
    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();
        }
    }
}

 

TrustSSL.trustEveryone();
Document doc = Jsoup.connect(url).data(key0,key).get();

 

以上是关于java的https请求解决证书问题的主要内容,如果未能解决你的问题,请参考以下文章

https证书到期会影响java请求么

用java做一个httpClient 发送https 的get请求,需要证书验证的那种,求大神指点一下!

Java# 请求https时证书不可信 PKIX SunCertPathBuilderException:unable to find valid certification....

解决局域网内开发https请求的证书问题

JDK版本不同引发的https请求证书问题

Java中HTTP接口请求与HTTPS证书验证问题