JVM / Android不信任StartSSL的根证书

时间:2015-10-21 12:34:03

标签: java android apache ssl https

我已经建立了一个用Apache托管两个网站的VPS。两者都具有带有StartSSL证书的(有效)SSL配置,我从桌面或移动浏览器访问它们没有问题。

我正在尝试访问其中一个网站使用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:无法找到所请求目标的有效证书路径< / p>

(如果我尝试访问https://google.com

,我当然无法获得

谷歌搜索后,我发现问题似乎是JVM(桌面)/ android无法识别StartSSL的根证书。我不想手动导入它,因为此代码的最终结果是在Android应用程序中运行。我不想通过允许任何证书来放弃SSL的所有利益,正如我在许多答案中看到的那样。

任何见解?

谢谢

1 个答案:

答案 0 :(得分:1)

如果您的平台不信任您的CA,那么除了手动将CA添加到平台的根CA信任列表或让您的证书由其中一个CA签名之外,您没有多少选择。你的平台信任。

相关问题