https请求使用OkHttpClient

huangapple go评论80阅读模式
英文:

https request using OkHttpClient

问题

为了测试目的,我正在使用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(); //获取JSON响应体
System.out.print(ResponseBody);

我收到的响应错误如下:

Exception in thread "main" javax.net.ssl.SSLHandshakeException:
sun.security.validator.ValidatorException: PKIX path validation
failed: java.security.cert.CertPathValidatorException: timestamp check
failed at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
...
at java.security.cert.CertificateExpiredException: NotAfter: Sat May 30
13:48:38 EAT 2020
...

英文:

For testing purpose I am sending https requests using OkHttpClient but the request keep on failing due to a certificate error, yet the endpoint I am sending to has a valid certificate.

Below is the code I am Using.

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);

The response error I am getting Is as below

> Exception in thread "main" javax.net.ssl.SSLHandshakeException:
> sun.security.validator.ValidatorException: PKIX path validation
> failed: java.security.cert.CertPathValidatorException: timestamp check
> failed at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
> at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1937) at
> sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302) at
> sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296) at
> sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1478)
> at
> sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:212)
> at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979) at
> sun.security.ssl.Handshaker.process_record(Handshaker.java:914) at
> sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1050) at
> sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1363)
> at
> sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1391)
> at
> sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1375)
> at
> okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:299)
> at
> okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:268)
> at
> okhttp3.internal.connection.RealConnection.connect(RealConnection.java:160)
> at
> okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:256)
> at
> okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:134)
> at
> okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:113)
> at
> okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
> at
> okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
> at
> okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93) at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
> at
> okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:125)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
> at
> okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
> at
> okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
> at okhttp3.RealCall.execute(RealCall.java:77) at
> com.impalapay.mno.servlet.api.bridge.coopbank.GetNewToken.main(GetNewToken.java:74)
> Caused by: sun.security.validator.ValidatorException: PKIX path
> validation failed: java.security.cert.CertPathValidatorException:
> timestamp check failed at
> sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:352)
> at
> sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:260)
> at sun.security.validator.Validator.validate(Validator.java:260) at
> sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)
> at
> sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)
> at
> sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)
> at
> sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1460)
> ... 27 more Caused by: java.security.cert.CertPathValidatorException:
> timestamp check failed at
> sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:129)
> at
> sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:212)
> at
> sun.security.provider.certpath.PKIXCertPathValidator.validate(PKIXCertPathValidator.java:140)
> at
> sun.security.provider.certpath.PKIXCertPathValidator.engineValidate(PKIXCertPathValidator.java:79)
> at
> java.security.cert.CertPathValidator.validate(CertPathValidator.java:292)
> at
> sun.security.validator.PKIXValidator.doValidate(PKIXValidator.java:347)
> ... 33 more Caused by:
> java.security.cert.CertificateExpiredException: NotAfter: Sat May 30
> 13:48:38 EAT 2020 at
> sun.security.x509.CertificateValidity.valid(CertificateValidity.java:274)
> at
> sun.security.x509.X509CertImpl.checkValidity(X509CertImpl.java:629)
> at
> sun.security.provider.certpath.BasicChecker.verifyTimestamp(BasicChecker.java:190)
> at
> sun.security.provider.certpath.BasicChecker.check(BasicChecker.java:144)
> at
> sun.security.provider.certpath.PKIXMasterCertPathValidator.validate(PKIXMasterCertPathValidator.java:119)
> ... 38 more

答案1

得分: 1

> 时间戳检查失败

客户端和服务器的时钟都正确吗?

英文:

> timestamp check failed

Do both client and server have correct clocks?

huangapple
  • 本文由 发表于 2020年7月24日 17:51:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63071138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定