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); } }
第二步:创建补充证书内部类:
文章评论