javahttpsurlconnectionjdk6

Java 1.6 HttpsURLConnection : java.net.SocketException: Connection reset




I,ve got a Problem here, I have some code that works perfect in 1.7 and above but once I switch to 1.6 I always get this error:

java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)

I think the problem is with the HttpsURLConnection but I can't figure out what. Here is how I initialize the 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);

And here is the Methode where the Error happens:
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);
                }
            }
        }

    }

}


I hope my problem is kinda clear and someone understands me :P
Thanks for taking your time to go over my problem!

EDIT

So I found out with Wireshark that the Packets that Fail are Smaller then the ones that succed (157 bytes = fail; 208 bytes = true)

I was thinking maybe he isnt encrypting the Data in the IP Packet oder isnt sending the Certificate.

I also noticed that the SSL Handshake is successful, but once the Client requests data it fails and the Server anwsers with:

Connection Reset

(Yes the Server is calling "Connection Reset")
I am really lost here :D


Solution

  • As @Robert mentioned in the comment:

    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.

    If you are unable to upgrade your Java version, you can use Apache HTTPClient.