java.net.URLConnection fails to connect via http or https

176 Views Asked by At

I am debugging an issue with my local Fiji/ImageJ installation failing to update through the Help -> Update UI. The ImageJ code that triggers the issue is this updater code: https://github.com/imagej/imagej-ui-swing/blob/78a3180b6bc830166d15c73a01443c7a642c3908/src/main/java/net/imagej/ui/swing/updater/ImageJUpdater.java#L360-L370

I wrote minimal test case to reproduce this pointing JAVA_HOME at the Fiji/ImageJ application (/Applications/Fiji.app/java/macosx/zulu8.60.0.21-ca-fx-jdk8.0.322-macosx_x64/jre/Contents/Home).

import java.net.URL;
import java.net.URLClassLoader;
import java.net.URLConnection;
import java.io.IOException;
import java.net.HttpURLConnection;

class HelloWorld {
    public static void main(String[] args) {
        try {
            final URL url = new URL("http://neverssl.com");
            final URLConnection urlConn = url.openConnection();
            final HttpURLConnection httpConn = (HttpURLConnection) urlConn;

            final int code = httpConn.getResponseCode();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }
}

This code results in the following error:

java.io.IOException: Error writing to server
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:705)
    at sun.net.www.protocol.http.HttpURLConnection.writeRequests(HttpURLConnection.java:717)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1598)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at HelloWorld.main(HelloWorld.java:14)

If I change the URL to an https URL, the error changes to

javax.net.ssl.SSLException: Couldn't kickstart handshaking
    at sun.security.ssl.Alert.createSSLException(Alert.java:127)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:348)
    at sun.security.ssl.TransportContext.fatal(TransportContext.java:291)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:449)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:410)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:197)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1577)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1505)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:480)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:352)
    at HelloWorld.main(HelloWorld.java:14)
    Suppressed: java.net.SocketException: Broken pipe (Write failed)
        at java.net.SocketOutputStream.socketWrite0(Native Method)
        at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
        at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
        at sun.security.ssl.SSLSocketOutputRecord.encodeAlert(SSLSocketOutputRecord.java:81)
        at sun.security.ssl.TransportContext.fatal(TransportContext.java:379)
        ... 10 more
Caused by: java.net.SocketException: Broken pipe (Write failed)
    at java.net.SocketOutputStream.socketWrite0(Native Method)
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:111)
    at java.net.SocketOutputStream.write(SocketOutputStream.java:155)
    at sun.security.ssl.SSLSocketOutputRecord.flush(SSLSocketOutputRecord.java:251)
    at sun.security.ssl.HandshakeOutStream.flush(HandshakeOutStream.java:89)
    at sun.security.ssl.ClientHello$ClientHelloKickstartProducer.produce(ClientHello.java:580)
    at sun.security.ssl.SSLHandshake.kickstart(SSLHandshake.java:510)
    at sun.security.ssl.ClientHandshakeContext.kickstart(ClientHandshakeContext.java:112)
    at sun.security.ssl.TransportContext.kickstart(TransportContext.java:231)
    at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:432)
    ... 8 more

Comparing a tcpdump filtered on the attempted host on this machine compared to a working one, I see early FIN packets being sent before any PSH packets are sent, so I think that is related to connections closing early.

On another machine running the same OS X version, I ran the same snippet pointing JAVA_HOME at the same directory, and had no issues.

0

There are 0 best solutions below