public static void main(String[] args) throws Exception {
System.setProperty("javax.net.ssl.keyStore","spacesalt.store");
System.setProperty("javax.net.ssl.keystorePassword", "123456");
HttpsURLConnection cons = null;
URL url = null;
String host ="https://www.google.com";
url = new URL(host) ;
cons = (HttpsURLConnection) url.openConnection() ;
cons.setReadTimeout(30000);
BufferedReader reader = new BufferedReader(new InputStreamReader(cons.getInputStream()));
String line = null;
StringBuilder data = new StringBuilder() ;
long startTime = System.currentTimeMillis() ;
while((line = reader.readLine()) != null) {
data.append(line +System.lineSeparator());
}
}
异常(exception)是 - java.security.NoSuchalgorithmException !我已经使用名为 spacesalt.store 的 keytool 创建了一个 keystore ,并将该 spacesalt.store 文件复制到项目中。我正在使用 eclipse ! 。确实,我对此一无所知。需要一个虚拟指南来解决它!
最佳答案
也许我重现了你的问题。我已经生成了一个 keystore 并执行了您的代码。之后我得到了一个异常(exception) 线程“main”中的异常 java.net.SocketException:
java.security.NoSuchAlgorithmException: Error constructing implementation (algorithm: Default, provider: SunJSSE, class: sun.security.ssl.SSLContextImpl$DefaultSSLContext)
at javax.net.ssl.DefaultSSLSocketFactory.throwException(SSLSocketFactory.java:248)
at javax.net.ssl.DefaultSSLSocketFactory.createSocket(SSLSocketFactory.java:255)
我解决了设置 keystore 的完整路径的问题:
System.setProperty("javax.net.ssl.keyStore","full_path_to_keyStore_/spacesalt.store");
对 keystore 的访问是通过调用 FileInputStream(property_value) 来完成的,因此,您还可以将 keystore 放在启动应用程序的文件夹中。
关于java.Security.NoSuchAlgorithmException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35523763/