Java 1.6 HttpsURLConnection:java.net.SocketException:连接重置
Java 1.6 HttpsURLConnection : java.net.SocketException: Connection reset
我在这里遇到了一个问题,我有一些代码在 1.7 及更高版本中运行完美但是一旦我切换到 1.6 我总是得到这个 error:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
我认为问题出在 HttpsURLConnection 上,但我不知道是什么。下面是我如何 初始化 HttpsURLConnection:
// create URL Object from String
URL url = new URL(https_url);
// set IP Adress for X509TrustManager
caTrustManager.setHostIP(url.getHost());
TrustManager[] trustCerts = new TrustManager[]{
caTrustManager
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustCerts, new java.security.SecureRandom());
//'ArrayList' where the data is saved from the URL
ArrayList data = null;
// open URL
HttpsURLConnection httpsVerbindung = (HttpsURLConnection) url.openConnection();
httpsVerbindung.setSSLSocketFactory(sc.getSocketFactory());
// Read the data from the URL
data = PerformAction.getContent(httpsVerbindung);
这是发生错误的方法:
Class = PerformAction
方法 = getContent(HttpsURLConnection con):
public static ArrayList getContent(HttpsURLConnection con) {
// The ArrayList where the information is saved
ArrayList infoFromTheSite = new ArrayList();
// check if connection not null
if (con != null) {
BufferedReader br = null;
try {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!! **Error happens Here** !!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
// go through all the Data
while ((input = br.readLine()) != null) {
// save to ArrayList
infoFromTheSite.add(input);
}
} catch (IOException ex) {
Logging.StringTimeFail("Fehler mit dem BufferedReaders");
ex.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(PerformAction.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
我希望我的问题很清楚并且有人理解我 :P
感谢您花时间解决我的问题!
编辑
所以我发现 Wireshark 失败的数据包比成功的数据包小(157 字节 = 失败;208 字节 = 真)
我在想也许他没有加密 IP 数据包中的数据或者没有发送证书。
我还注意到 SSL 握手是成功的,但是一旦客户端请求数据,它就会失败,并且服务器会回复:
Connection Reset
(是的,服务器正在调用 "Connection Reset")
我真的迷路了:D
正如@Robert 在评论中提到的:
If the server you are communicating with is securely maintained you
will have problems with Java 1.6 because it does only support the
insecure versions of SSL/TLS (no TLS 1.1 and 1.2 support and only
outdated ciphers).
Fix: Upgrade your Java version.
如果您无法升级您的 Java 版本,您可以使用 Apache HTTPClient.
我在这里遇到了一个问题,我有一些代码在 1.7 及更高版本中运行完美但是一旦我切换到 1.6 我总是得到这个 error:
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
我认为问题出在 HttpsURLConnection 上,但我不知道是什么。下面是我如何 初始化 HttpsURLConnection:
// create URL Object from String
URL url = new URL(https_url);
// set IP Adress for X509TrustManager
caTrustManager.setHostIP(url.getHost());
TrustManager[] trustCerts = new TrustManager[]{
caTrustManager
};
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustCerts, new java.security.SecureRandom());
//'ArrayList' where the data is saved from the URL
ArrayList data = null;
// open URL
HttpsURLConnection httpsVerbindung = (HttpsURLConnection) url.openConnection();
httpsVerbindung.setSSLSocketFactory(sc.getSocketFactory());
// Read the data from the URL
data = PerformAction.getContent(httpsVerbindung);
这是发生错误的方法:
Class = PerformAction
方法 = getContent(HttpsURLConnection con):
public static ArrayList getContent(HttpsURLConnection con) {
// The ArrayList where the information is saved
ArrayList infoFromTheSite = new ArrayList();
// check if connection not null
if (con != null) {
BufferedReader br = null;
try {
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !!!! **Error happens Here** !!!!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String input;
// go through all the Data
while ((input = br.readLine()) != null) {
// save to ArrayList
infoFromTheSite.add(input);
}
} catch (IOException ex) {
Logging.StringTimeFail("Fehler mit dem BufferedReaders");
ex.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException ex) {
Logger.getLogger(PerformAction.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
我希望我的问题很清楚并且有人理解我 :P
感谢您花时间解决我的问题!
编辑
所以我发现 Wireshark 失败的数据包比成功的数据包小(157 字节 = 失败;208 字节 = 真)
我在想也许他没有加密 IP 数据包中的数据或者没有发送证书。
我还注意到 SSL 握手是成功的,但是一旦客户端请求数据,它就会失败,并且服务器会回复:
Connection Reset
(是的,服务器正在调用 "Connection Reset")
我真的迷路了:D
正如@Robert 在评论中提到的:
If the server you are communicating with is securely maintained you will have problems with Java 1.6 because it does only support the insecure versions of SSL/TLS (no TLS 1.1 and 1.2 support and only outdated ciphers).
Fix: Upgrade your Java version.
如果您无法升级您的 Java 版本,您可以使用 Apache HTTPClient.