Java网络请求,okhttpclient中https的证书是有时间限制的,如何解决?

2023年10月3日 365点热度 0人点赞 0条评论

Java网络请求,okhttpclient中https的证书是有时间限制的,如何解决?
第一步:在请求示例:

public static void main(String[] args){
    String requestUrl = "https://请求地址";
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new java.security.SecureRandom());

        OkHttpClient client = new OkHttpClient().newBuilder()
                .sslSocketFactory(sc.getSocketFactory())
                .connectTimeout(5000, TimeUnit.MILLISECONDS)
                .build();

        Request request = new Request.Builder()
                .url(requestUrl)
                .build();
       Response response = client.newCall(request).execute();
       String result = response.body().toString();
    } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
    } catch (KeyManagementException e) {
            throw new RuntimeException(e);
    }
}

第二步:创建补充证书内部类:

支付 ¥2.99 购买本节后解锁剩余30%的内容

小小调酒师

此刻打盹,你将做梦; 此刻学习,你将圆梦。 个人邮箱:shellways@foxmail.com

文章评论