Search code examples
javahttpurlconnectionsocketexceptionconnection-reset

Why do I keep getting a java.net.SocketException: Connection reset error?


I am running a Java Class GetURL.java which takes a URL and returns the HTML of that page, however I keep getting the following output when trying a particular URL:

Reading URL: https://artifactory.somewhere.com/artifactory/webapp
url.getHost: artifactory.somewhere.com
Socket Inet Address: artifactory.somewhere.com/XX.XXX.XX.XX
https://artifactory.somewhere.com/artifactory/webapp is reachable: false
HTTP URL Connection (huc) is using a proxy: false
Can not connect
java.net.SocketException: Connection reset
        at java.net.SocketInputStream.read(SocketInputStream.java:208)
        at java.net.SocketInputStream.read(SocketInputStream.java:134)
        at com.ibm.jsse2.a.a(a.java:227)
        at com.ibm.jsse2.a.a(a.java:269)
        at com.ibm.jsse2.qc.a(qc.java:459)
        at com.ibm.jsse2.qc.h(qc.java:275)
        at com.ibm.jsse2.qc.a(qc.java:541)
        at com.ibm.jsse2.qc.startHandshake(qc.java:89)
        at com.ibm.net.ssl.www2.protocol.https.c.afterConnect(c.java:173)
        at com.ibm.net.ssl.www2.protocol.https.d.connect(d.java:63)
        at com.ibm.net.ssl.www2.protocol.https.b.connect(b.java:96)
        at GetURL.getURL(GetURL.java:45)
        at GetURL.main(GetURL.java:78)

The Java Class is:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Map;
import java.net.Socket;
/**
 * Chapter 2 Example
 *
 * This program uses the standard Java URL class to open a connection to a web
 * page and download the contents.
 *
 * @author Jeff Heaton
 * @version 1.0
 */
public class GetURL {
    /**
     * This method will display the URL specified by the parameter.
     *
     * @param u
     *            The URL to display.
     */
    static protected void getURL(String u) {
        URL url;
        InputStream is;
        InputStreamReader isr;
        BufferedReader r;
        String str;
        try {
            System.out.println("Reading URL: " + u);
            url = new URL(u);
            System.out.println("url.getHost: " + url.getHost());
            Socket clientSocket = new Socket(url.getHost(), 443);
            System.out.println("Socket Inet Address: " + clientSocket.getInetAddress());
            Boolean canReachClient = clientSocket.getInetAddress().isReachable(5 * 1000);
            System.out.println(u + " is reachable: " + canReachClient);
            HttpURLConnection huc = (HttpURLConnection) url.openConnection();
            HttpURLConnection.setFollowRedirects(true);
            huc.setConnectTimeout(5 * 1000);
            huc.setRequestMethod("GET");
            System.out.println("HTTP URL Connection (huc) is using a proxy: " + huc.usingProxy());
            huc.connect(); // problem is HERE!!!!
            is = huc.getInputStream();
            isr = new InputStreamReader(is);
            r = new BufferedReader(isr);
            do {
                str = r.readLine();
                if (str != null)
                    System.out.println(str);
            } while (str != null);
        } catch (MalformedURLException e) {
            System.out.println("Must enter a valid URL");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("Can not connect");
            e.printStackTrace();
        }
    }
    /**
     * Program entry point.
     *
     * @param args
     *            Command line arguments. Specified the URL to download.
     */
    static public void main(String args[]) {
        if (args.length < 1)
            System.out.println("Usage: GetURL [url]");
        else
            getURL(args[0]);
    }
}

I've managed to identify that the error occurs on when calling huc.connect(); but can't figure out the cause.

I'm using jdk1.7.1_64-ibm and it works as expected with other HTTPS addresses

Thanks in advance for any assistance


Solution

  • I had a colleague who ran into an issue like this.

    He found out that it was because the server he was calling had removed support for TLS 1.0 but java 7 was defaulting to use TLS 1.0.

    He fixed it by forcing java to use TLS 1.2.

    java -Dhttps.protocols=TLSv1.2 GetURL https://artifactory.somewhere.com/artifactory/webapp