【问题标题】:Java 1.6 HttpsURLConnection : java.net.SocketException: Connection resetJava 1.6 HttpsURLConnection:java.net.SocketException:连接重置
【发布时间】:2015-11-26 08:56:11
【问题描述】:



我在这里遇到了一个问题,我有一些代码在 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);

这是发生错误的 Methode:
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

【问题讨论】:

  • 如果与您通信的服务器得到安全维护,您将遇到 Java 1.6 问题,因为它仅支持 SSL/TLS 的不安全版本(不支持 TLS 1.1 和 1.2,仅支持过时的密码)。修复:升级您的 Java 版本。
  • 问题是,我不能。该应用程序必须在公司的计算机网络上运行,并且他们只有 Java 1.6,而且它看起来不会很快更新,因为它只是最近。看起来我必须用 C# 重写这个东西:D 谢谢你的提醒顺便说一句! ;)
  • @Robert 您能否写下该评论作为答案,因为我认为如果其他程序员能够看到这是 Java 1.6 的问题,那将会很有帮助。干杯!
  • @JoCoaker 对此不确定,但您也许可以使用 apache HTTPClient。
  • @KDM 感谢您的提示。我检查了它,但我真的不明白它是如何工作的。我想我得继续努力了

标签: java httpsurlconnection jdk6


【解决方案1】:

正如@Robert 在评论中提到的:

如果您与之通信的服务器得到安全维护,您 Java 1.6 会有问题,因为它只支持 SSL/TLS 的不安全版本(不支持 TLS 1.1 和 1.2,仅 过时的密码)。

修复:升级您的 Java 版本。

如果您无法升级您的 Java 版本,您可以使用 Apache HTTPClient

【讨论】:

  • 我遇到了与 jdk-1.7.0_51 类似的问题。升级到 1.8 为我解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2013-06-12
  • 2019-12-08
  • 1970-01-01
  • 1970-01-01
  • 2011-10-08
  • 2013-03-14
  • 2012-08-02
  • 1970-01-01
相关资源
最近更新 更多