Java 1.6 HttpsURLConnection : java.net.SocketException:连接重置

标签 java httpsurlconnection jdk6



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

我在想也许他没有加密 IP 数据包中的数据或没有发送证书。

我还注意到 SSL 握手是成功的,但是一旦客户端请求数据,它就会失败并且服务器会回答:

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

关于Java 1.6 HttpsURLConnection : java.net.SocketException:连接重置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32326228/

相关文章:

java - 方法名称总是必须是动词吗?

java - 无法访问 while 循环内初始化的数组

java - HttpUrlConnection 到 HttpsUrlConnection,我可以只添加 's' 吗?

java - 接收对 HttpsURLConnection GET 请求的编码响应

ant xjc 任务与 jdk 6 一起使用

java - 传送到下一个玩家

java - android - httpsurlconnection 'get' 405 - 方法不允许

java - 为什么 ProcessBuilder 类不重写 equals()?

java - 使用 JDK6 的 JBOSS 5.1 中的 Web 服务客户端

java - 仅图像的单独项目 - 需要重构吗?