我正在尝试实现最简单的HTTPS通信程序。网络上有很多例子,但我无法成功运行它们。
这是一个例子:
public class ReadHttpsURL1 {
static final int HTTPS_PORT = 443;
public static void main(String argv[]) throws Exception {
String url = "www.sun.com";
System.setProperty("java.net.useSystemProxies", "true");
SocketFactory factory = SSLSocketFactory.getDefault();
Socket socket = factory.createSocket(url, HTTPS_PORT);
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(socket.getOutputStream()));
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
out.write("GET / HTTP/1.0\n\n");
out.flush();
String line;
StringBuffer sb = new StringBuffer();
while((line = in.readLine()) != null) {
sb.append(line);
}
out.close();
in.close();
System.out.println(sb.toString());
}
}
它会在大约一分钟左右挂起(我认为是由于服务器端超时),然后出现错误。挂起在
Socket socket = factory.createSocket(url, HTTPS_PORT);
错误是
Exception in thread "main" java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocksSocketImpl.readSocksReply(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.connect(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.<init>(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketFactoryImpl.createSocket(Unknown Source)
at com.sun.ex1.ReadHttpsURL1.main(ReadHttpsURL1.java:20)
这是第二个例子:
public class HttpsTest {
public static void main(String[] args) throws MalformedURLException, IOException {
System.setProperty("java.net.useSystemProxies", "true");
URL url = new URL("https://www.sun.com");
URLConnection con = url.openConnection();
con.setAllowUserInteraction(true);
InputStream is = new BufferedInputStream(con.getInputStream());
for (int b = is.read(); b >= 0; b = is.read()) {
System.out.write(b);
}
}
}
只需抛出此异常:
Exception in thread "main" javax.net.ssl.SSLKeyException: RSA premaster secret error
at com.sun.net.ssl.internal.ssl.RSAClientKeyExchange.<init>(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source)
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at HttpsTest.main(HttpsTest.java:16)
Caused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available
at javax.crypto.KeyGenerator.<init>(DashoA13*..)
at javax.crypto.KeyGenerator.getInstance(DashoA13*..)
at com.sun.net.ssl.internal.ssl.JsseJce.getKeyGenerator(Unknown Source)
... 14 more
我已经阅读了一些NoSuchAlgorithmException的相关信息。这似乎与sunjce_provider.jar有关。
我尝试了不同的变体来包含这个文件在类路径中,甚至将应用程序的依赖性附加到这个jar(可以确定这个jar已经存在于类路径中)
HTTPS URL是活动URL。代理设置有效(否则会引发不同的连接异常)。
java版本"1.6.0_23"
这些错误是否相关?
有什么想法如何解决?
感谢您的帮助!