3
votes

I have the following simple code to establish an SSL connection.

public static void main(String[] args) throws IOException {
    
    System.setProperty("javax.net.debug", "ALL");
    System.setProperty("java.net.useSystemProxies", "true");
    
    URL url = new URL("https://google.com");
    URLConnection con = url.openConnection();
    try (BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()))) {
        String line = null;
        while ((line = br.readLine()) != null) {
            System.out.println(line);
        }
    }
}

If I run this code under Java 8 it doesn't work. It stops at starting the SSL Handshake with a java.net.SocketException: Connection reset.

main, WRITE: TLSv1.2 Handshake, length = 214 main, waiting for close_notify or alert: state 1 Exception in thread "main" java.net.SocketException: Connection reset at java.net.SocketInputStream.read(SocketInputStream.java:209) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.security.ssl.InputRecord.readFully(InputRecord.java:465) at sun.security.ssl.InputRecord.read(InputRecord.java:503) at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:973) at sun.security.ssl.SSLSocketImpl.waitForClose(SSLSocketImpl.java:1769) at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:124) at sun.security.ssl.Handshaker.kickstart(Handshaker.java:1020) at sun.security.ssl.SSLSocketImpl.kickstartHandshake(SSLSocketImpl.java:1487) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1351) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403) at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387) at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:563) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185) at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1512) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1440) at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254) at com.hlcl.exp.Main.main(Main.java:18) main, Exception while waiting for close java.net.SocketException: Connection reset main, handling exception: java.net.SocketException: Connection reset main, SEND TLSv1.2 ALERT: fatal, description = unexpected_message main, WRITE: TLSv1.2 Alert, length = 2 main, Exception sending alert: java.net.SocketException: Connection reset by peer: socket write error main, called closeSocket()

If I execute the same code in a Java 10 environment it works.

I have checked the proxy which is detected by java. Under Java 8 a DIRECT (no proxy) connection is detected. With Java 10 the following proxy settings are detected.

proxy type: HTTP

proxy hostname: ipv4.xxx.xxx.xxx.com

proxy port: 8081

If I remove System.setProperty("java.net.useSystemProxies", "true"); or set it to false it also not work with Java 10. But setting the proxy settings via the system properties is not working.

2

2 Answers

2
votes

I think that the proxy detection in Java 8 seems not to work for you. Assuming the detection of Java 10 is correct try to configure your proxy via the system properties (for HTTPS Connections).

System.setProperty("https.proxyHost", "<proxy host>");
System.setProperty("https.proxyPort", "<proxy port>");
0
votes

I'm not a Java expert. But for me your question essentially boils down to:

  • it is not working if no proxy is used (probably because of some firewall)
  • it works (at least with Java 10) if a proxy is used
  • but you cannot make it use a proxy with Java 8 - at least not the same way as with Java 10

How you can set a proxy in Java 8 is documented in Java Networking and Proxies. java.net.useSystemProxies seems to be supported with Java 8 too according to the documentation. So it might be that you execute your program on different systems (hence different Java versions) and that one has a proper system proxy set while the other has not.