我在这里遇到了一个问题,我有一些在 1.7 及更高版本中完美运行的代码,但是一旦我切换到 1.6,我总是会收到这个错误:
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
Methode = 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
(是的,服务器正在调用“连接重置”)
我真的迷路了:D