【发布时间】:2016-04-26 16:16:30
【问题描述】:
我必须在没有任何参数的情况下向某些 API 发送 GET 请求,所以我编写了代码:
public Boolean getData(String apiUrl) {
try {
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> responseEntity = restTemplate.getForEntity(apiUrl, String.class);
if (responseEntity.getStatusCode().toString().equals("200")) {
theLogger.info("Server successfully answered with response code: {} - {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return true;
} else {
theLogger.error("Something goes wrong! Server answered with response code: {} and error: {}", responseEntity.getStatusCode().toString(), responseEntity.getBody());
return false;
}
} catch (Exception theException) {
theException.printStackTrace();
theLogger.error("Cannot send channelInfo! Exception: " + theException);
return false;
}
}
API url 为 HTTP 时有效,但不适用于 HTTPS。它说:
sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径;嵌套异常是 javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
我相信这是因为我的代码不信任此证书。但我不知道我该怎么办? API 是公开的,它可以在任何网络浏览器中打开,它只是一些 https 站点。我简直不敢相信我应该为我使用的所有 https 站点下载证书并将此证书添加到某处。我相信这应该是信任这个公共网站的更常见的方式。
【问题讨论】:
标签: java spring rest ssl https