【发布时间】:2020-11-14 04:13:32
【问题描述】:
出于测试目的,我使用 OkHttpClient 发送 https 请求,但由于证书错误,请求不断失败,但我发送到的端点具有有效证书。
下面是我正在使用的代码。
String auth = "YYYYY";
OkHttpClient client;
client = new OkHttpClient();
Request request = new Request.Builder()
.url("https://...........")
.get()
.addHeader("authorization", "Basic " + auth)
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request).execute();
String ResponseBody=response.body().string(); //get json response body
System.out.print(ResponseBody);
我得到的响应错误如下
线程“主”javax.net.ssl.SSLHandshakeException 中的异常: sun.security.validator.ValidatorException:PKIX 路径验证 失败:java.security.cert.CertPathValidatorException:时间戳检查 在 sun.security.ssl.Alerts.getSSLException 失败(Alerts.java:192) 在 sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) 在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) 在 sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) 在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478) 在 sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212) 在 sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) 在 sun.security.ssl.Handshaker.process_record(Handshaker.java:914) 在 sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) 在 sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391) 在 sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375) 在 好的http3.internal.connection.RealConnection.connectTls(RealConnection.java:299) 在 okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:268) 在 okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160) 在 好的http3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256) 在 好的http3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134) 在 okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113) 在 okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147) 在 okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121) 在 okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200) 在 okhttp3.RealCall.execute(RealCall.java:77) 在 com.impalapay.mno.servlet.api.bridge.coopbank.GetNewToken.main(GetNewToken.java:74) 引起:sun.security.validator.ValidatorException:PKIX 路径 验证失败:java.security.cert.CertPathValidatorException: 时间戳检查失败 sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:352) 在 sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:260) 在 sun.security.validator.Validator.validate(Validator.java:260) 在 sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324) 在 sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229) 在 sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124) 在 sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460) ... 27 更多原因:java.security.cert.CertPathValidatorException: 时间戳检查失败 sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:129) 在 sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:212) 在 sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:140) 在 sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:79) 在 java.security.cert.CertPathValidator.validate(CertPathValidator.java:292) 在 sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:347) ... 33 更多原因: java.security.cert.CertificateExpiredException:NotAfter:5 月 30 日星期六 13:48:38 吃 2020 在 sun.security.x509.CertificateValidity.valid(CertificateValidity.java:274) 在 sun.security.x509.X509CertImpl.checkValidity(X509CertImpl.java:629) 在 sun.security.provider.certpath.BasicChecker.verifyTimestamp(BasicChecker.java:190) 在 sun.security.provider.certpath.BasicChecker.check(BasicChecker.java:144) 在 sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:119) ... 38 更多
【问题讨论】:
-
您需要将此证书添加到您的
jretruststore。 -
如何在上述请求中做到这一点?
-
也许this 会有所帮助。