我需要使用用Java编写的客户端调用HTTP Rest POST服务。使用POSTMAN甚至Java客户端,该调用都可以正常工作。 但有时我会出现以下错误:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
我无法理解为什么有时调用失败并且有时调用失败总是失败?
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(url);
StringEntity input = new StringEntity(json, "UTF-8");
input.setContentType("application/json;charset=UTF-8");
postRequest.setEntity(input);
String encoding = Base64.encodeBase64String(new String("user:password").getBytes());
postRequest.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + encoding);
HttpResponse response = httpClient.execute(postRequest);
请不要将其标记为重复项。 我遇到的这个问题是针对HTTP呼叫,而不是HTTPS。 我想知道有时但并非总是失败的原因,如果原因是未添加证书。