我已经建立了一个VPS,它使用Apache托管两个网站。两者都有一个带有StartSSL证书的(有效的)SSL配置,我从桌面或移动浏览器访问它们没有问题。JVM/Android不受信任的StartSSL的根证书
我想访问其中一个网站使用SSL运行的API,但我遇到了问题。我第一次使用Apache HttpClient(已弃用),但it looks like it can't choose the proper certificate on the server because it doesn't support Server Name Indication,并且解决方法是使用HttpsURLConnection。
所以我现在有这个代码,无耻地从互联网上复制:
String url = "https://mywebsite.ext/api/xxx";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
String urlParameters = "blah=foo&bar=xx";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
// ...
而我得到的是:
异常线程 “main” javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的证书路径以请求目标
(当然,如果我尝试访问例如https://google.com我不明白)
谷歌搜索后,我发现,这个问题似乎是startssl如果的根证书不是由JVM(桌面)认可/ android。我不想手动导入它,因为此代码的终结点是在Android应用程序中运行。我不想通过允许任何证书来放弃SSL的所有兴趣,正如我在许多答案中所看到的。
任何见解?
谢谢
您是否正在提供正确的中间证书?您可以使用SSL Labs SSL Test等工具来查看是否发送构建证书链所需的所有中间证书。 –
是的,中间证书被正确发送。检查SSL实验室 – christophetd