java https网络问题 [英] java https networking issue
问题描述
我正在尝试实现最简单的HTTPS通信程序.网络上有很多示例,但我无法成功运行它们.
这是一个示例:
I'm trying to implement simplest HTTPS communication program. There are a lot of examples on the web, but I fail to run them successfully.
Here is one example:
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());
}
}
它挂起大约一分钟的时间(我相信是由于服务器端超时),并因错误而失败.挂断电话
It hangs up for a time about a minute (I believe due to server-side timeout) and fail with error. Hangs up on
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
I've read a bit about NoSuchAlgorithmException. This seems to be related with sunjce_provider.jar
I've tried different variants to include this file in the classpath and even make application dependency to this jar (jar was present in classpath for sure)
HTTPS URL是实时URL.代理服务器设置有效(否则会引发其他连接异常).
Java版本"1.6.0_23"
HTTPS URL is live URL. Proxy settings are working (it throws a different connection exception otherwise).
java version "1.6.0_23"
这些错误是否相关?
有任何解决方法的想法吗?
感谢您的帮助!
Are these errors related?
Any ideas how to fix?
Thanks for help!
推荐答案
套接字出现的问题是由代理引起的.如果我没有得到 java.net.ConnectException,我的结论是错误的:连接被拒绝:connect (以防根本没有配置代理)代理不会引起问题.在来宾网络"(无代理)中,应用程序似乎正在运行.事实证明,代理对于每个协议具有不同的配置,并且我的公司政策不赞成使用原始套接字连接.
Issue with sockets was caused by proxy. I've wrong concluded if I don't get java.net.ConnectException: Connection refused: connect (in case proxy is not configured at all) proxy should not cause issue. In a 'guest network' (without proxy) application appeared to be working. As it turned out proxy had different configuration for each protocol and my company policy deprecate raw sockets connections.
这篇关于java https网络问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!