【发布时间】:2016-01-20 10:59:58
【问题描述】:
我已经设置了一个 VPS,它使用 Apache 托管两个网站。两者都有一个(有效的)带有 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();
// ...
我得到的是:
线程“主”javax.net.ssl.SSLHandshakeException 中的异常:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
(如果我尝试访问例如https://google.com,当然我不会得到)
谷歌搜索后,我发现问题似乎是JVM(桌面)/android无法识别StartSSL的根证书。我不想手动导入它,因为这段代码的最终目的是在 Android 应用程序中运行。正如我在许多答案中看到的那样,我不想通过允许任何证书来失去对 SSL 的所有兴趣。
有什么见解吗?
谢谢
【问题讨论】:
-
您是否提供正确的中间证书?您可以使用 SSL Labs SSL Test 之类的工具来查看您是否发送了构建证书链所需的所有中间证书。
-
是的,中间证书发送正确。通过 SSL 实验室检查
标签: java android apache ssl https