1
我要送GET请求一些API不带任何参数,所以我写的代码:Java-Spring:如何向http url发送http请求?
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 path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is 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
我相信这是因为我的代码不信任此证书。但我不知道该怎么办? API是公开的,它在任何网页浏览器中打开,它只是一些https网站。我无法相信我应该为所有与我一起工作的https站点下载证书,并在某处添加此证书...我相信它应该是更常见的方式来信任这些公共站点。请有人给我一个援助之手吗?