java - 从 URL 获取数据时 PKIX 路径构建失败

标签 java ssl ssl-certificate

我正在尝试获取如下数据(通过 API 指示):

HttpResponse<String> response = Unirest.get("https://opendata.aemet.es/opendata/api/valores/climatologicos/diarios/datos/fechaini/2017-08-16T00:00:00UTC/fechafin/2017-08-17T23:59:59UTC/estacion/2422/?api_key=eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJqb3NoZXJAY2FydGlmLmVzIiwianRpIjoiZWM3MDkxMzItNTE0ZC00OGZjLTljMjMtYTBiMzVjNTllYzdjIiwiZXhwIjoxNTEwNzMxMzU4LCJpc3MiOiJBRU1FVCIsImlhdCI6MTUwMjk1NTM1OCwidXNlcklkIjoiZWM3MDkxMzItNTE0ZC00OGZjLTljMjMtYTBiMzVjNTllYzdjIiwicm9sZSI6IiJ9.WSVaE3C9TFMMa8x1mUvHpt1-n3Cgy3p7r2DaflSm9VU")
              .header("cache-control", "no-cache")
              .asString();

然而,当我执行代码时,抛出异常:

Exception in thread "main" com.mashape.unirest.http.exceptions.UnirestException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:143)
    at com.mashape.unirest.request.BaseRequest.asString(BaseRequest.java:56)
    at eu.cartif.meteo.Weather_Meteo.main(Weather_Meteo.java:38)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.ssl.Alerts.getSSLException(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.Handshaker.fatalSE(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:396)
    at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:355)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:142)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:359)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:381)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:237)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:185)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:111)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
    at com.mashape.unirest.http.HttpClientHelper.request(HttpClientHelper.java:138)
... 2 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.validator.PKIXValidator.doBuild(Unknown Source)
    at sun.security.validator.PKIXValidator.engineValidate(Unknown Source)
    at sun.security.validator.Validator.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source)
    at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)
... 24 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at sun.security.provider.certpath.SunCertPathBuilder.build(Unknown Source)
    at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source)
    at java.security.cert.CertPathBuilder.build(Unknown Source)
... 30 more    

我也曾尝试通过 key 工具导入证书,但我收到一条消息,提示“文件 keystore 不存在”。

有人可以支持我吗?提前致谢

最佳答案

使用浏览器 5 秒给了我所有这些信息:

Your connection is not secure

The owner of opendata.aemet.es has configured their web site improperly. To protect your information from being stolen, Firefox has not connected to this web site.

Learn more…

Report errors like this to help Mozilla identify and block malicious sites

opendata.aemet.es uses an invalid security certificate.

The certificate is not trusted because the issuer certificate is unknown.
The server might not be sending the appropriate intermediate certificates.
An additional root certificate may need to be imported.

Error code: SEC_ERROR_UNKNOWN_ISSUER

关于java - 从 URL 获取数据时 PKIX 路径构建失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45731942/

相关文章:

java - 使用 jsoup 获取验证码图像

c++ - 如何使用相应的 X509 证书验证私有(private) RSA 签名

xcode - 验证应用错误 : An SSL error has occurred and secure connection to the server cannot be made

SSL 创建算法

c# - 使用具有多个证书和私钥的 PEM 文件

java - 更新数组列表

java - 总结列表的每 N 个元素?

java - 使用monit监控一个java程序

从 certbot 删除 ssl 证书后,Apache 没有启动

.net - Windows 10 通用应用程序中的证书固定