I am trying to get content from a HTTPS web service from Java:
URL url = new URL(siteUrl);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setReadTimeout(5000);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuffer buffer = new StringBuffer();
while ((line = rd.readLine()) != null) {
buffer.append(line);
}
String res = buffer.toString();
It works nicely. But after some time my app has been running (after 18 hours or so), I get SSL exception:
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
I understand that my "jre/lib/security/cacerts" file should contain trusted certs, and I added the cert from the web service. It doesn't help. Still same issue. Restarting the java app helps for about 18 hours, but then the PKIX error shows again.
The web service is handled by WordPress and in front of it is an nginx with some caching enabled.
Everybody is suggesting that correct cert be included in the cacerts file. But the odd thing is, then why only throw error after some time, why not the first time it is called?
Any ideas very much appretiated.