2011-07-11 43 views
2

我试图使用HttpsURLConnection与网站进行HTTPS连接,然后执行PUT请求。当我尝试从HttpsURLConnection.getOutputStream()OutputStreamWriter,以下异常被抛出:Java HttpsURLConnection SSLHandshakeException

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 

页刚刚得到的证书(由StartCom发行) - 做我需要做手工的东西让Java识别证书现在呢?我可以毫不费力地连接到同一网站的其他页面,但他们拥有不同的证书。

回答

5

您可以将StartCom yia工具keytool(来自JDK)的根证书导入Java密钥库(JKS),然后将密钥库设置为“可信存储区”。

见 “导入和导出证书” 上:

导入certifificate到trustedcerts.jks:该文章中提到的

http://java.sun.com/developer/technicalArticles/Security/secureinternet2/

命令

keytool -import -keystore trustedcerts.jks -alias qusay -file server.crt 

开始使用Java定制tuststore:

java -Djavax.net.ssl.trustStore=trustedcerts.jks com.example.MyClass 

您也可以在运行时设置的信任:

System.setProperty("javax.net.ssl.trustStore","./trustedcerts.jks"); 
+1

谢谢 - 对于有同样问题的其他人,我在本地保存证书,使用'keytool -import ...'命令并添加了'System.setProperty(“javax.net.ssl.trustStore”,“path_to_keystore” );'我的代码。 – caroline

+1

我将不同的命令复制到我的答案中。 – Robert