Java 使用OkHttpClient的https请求
出于测试目的,我正在使用OkHttpClient发送https请求,但由于证书错误,请求继续失败,但我发送到的端点具有有效的证书 下面是我正在使用的代码Java 使用OkHttpClient的https请求,java,ssl,okhttp,Java,Ssl,Okhttp,出于测试目的,我正在使用OkHttpClient发送https请求,但由于证书错误,请求继续失败,但我发送到的端点具有有效的证书 下面是我正在使用的代码 String auth = "YYYYY"; OkHttpClient client; client = new OkHttpClient(); Request request = new Request.Builder() .url("https://...........&quo
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);
我得到的响应错误如下所示
线程“main”javax.net.ssl.SSLHandshakeException中的异常:
sun.security.validator.validator异常:PKIX路径验证
失败:java.security.cert.CertPathValidator异常:时间戳检查
在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)位于
server证书(ClientHandshaker.java:1478)
在
processMessage(ClientHandshaker.java:212)
位于sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)
sun.security.ssl.Handshaker.process_记录(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)
在
okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:299)
在
okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:268)
在
okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
在
okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
在
okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
在
okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
在
okhttp3.internal.connection.ConnectionInterceptor.intercept(ConnectionInterceptor.java:42)
在
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:147)
在
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:121)
在
okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
在
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:147)
在
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:121)
在
okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)位于
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:147)
在
okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
在
okhttp3.internal.http.RealInterceptorChain.procedure(RealInterceptorChain.java:147)
在
okhttp3.internal.http.RealInterceptorChain.procedure(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.validator异常:PKIX路径
验证失败:java.security.cert.CertPathValidator异常:
时间戳检查失败
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)
在
server证书(ClientHandshaker.java:1460)
... 27更多原因:java.security.cert.CertPathValidator异常:
时间戳检查在上失败
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多
时间戳检查失败
客户端和服务器是否都有正确的时钟?您需要将此证书添加到您的
jre
信任库中。在上述请求中,我该如何操作?可能会有所帮助。