0

I am trying to check weather my jboss server is up or not or simply url is reachable or not. I am achieving same using responseCode i.e. 200 for OK. I am able to do for HTTP but fails for HTTPS. So I want the responseCode for HTTPS.

I am using below code

        HttpURLConnection urlCon = null;
        URL url = new URL("http://localhost:8080/abc");
        urlCon = (HttpURLConnection) url.openConnection();
        System.out.println(urlCon.getResponseCode());
        if(urlCon.getResponseCode() == 200)
        {
            System.out.println("Server is up");
        }

works fine for HTTP but when I am trying same for HTTPS It gives connection refuse or SSLhandshakeException

javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:882)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1188)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1215)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1199)
    at sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:434)
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:166)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1195)
    at java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:379)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getResponseCode(HttpsURLConnectionImpl.java:318)
    at com.de.base.tools.Sample.main(Sample.java:23)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
    at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:462)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:863)
    ... 9 more

I have tried HttpURLConnection to HttpsURLConnection but gives same

anybody know how to get responceCode() for HTTPS

Thanks.

Hitesh Ghuge
  • 793
  • 2
  • 10
  • 39
  • Are you using the correct port for HTTPS? Servers that have HTTP on 8080 _usually_ have HTTPS on 8443 (although nothing requires this). – dave_thompson_085 Jul 18 '16 at 12:31
  • I have config apache and is listing port is 443 – Hitesh Ghuge Jul 18 '16 at 12:58
  • You said you're trying to connect to Jboss, not Apache. Which is it? Apache typically uses the standard ports, 80 for HTTP and 443 for HTTPS; Jboss typically does not. – dave_thompson_085 Jul 18 '16 at 23:35
  • apache is my load balancer and I config server to HTTPS using it – Hitesh Ghuge Jul 19 '16 at 05:29
  • So Apache is listening _for SSL_ on 443, and forwarding to Jboss? And your Java is connecting to the hostname where Apache runs, and port 443, and getting this error? If so: (1) try the `javax.net.debug` log as Hrabosh said and add it to your Q (don't put critical information in comments, they are sometimes deleted) (2) Try a browser (or several browsers) or other clients (curl, wget, openssl s_client, gnutls-cli) connecting to the Apache hostname and port 443. – dave_thompson_085 Jul 19 '16 at 22:10

1 Answers1

1

I think it is problem with self-signed certs which are not in your trust store. You can check it when you run your client with this: -Djavax.net.debug=ssl Its enable debug for ssl and you will have a better output of error. If you want to fix problem with self-signed cert, you have to add it to your trusted Java key store. I think in this question are your answers for this.

Community
  • 1
  • 1
Hrabosch
  • 1,541
  • 8
  • 12
  • Error still persist – Hitesh Ghuge Jul 18 '16 at 10:45
  • I referred [This](http://stackoverflow.com/questions/1828775/how-to-handle-invalid-ssl-certificates-with-apache-httpclient) – Hitesh Ghuge Jul 18 '16 at 10:59
  • Some SSL/TLS errors are caused by certificates, of all kinds both self-signed and CA-signed, but some are not, and if you look at the actual exception in this case it is absolutely not caused by any certificate. – dave_thompson_085 Jul 18 '16 at 12:32
  • @dave_thompson_085 Why not? I wrote just I think because it is most common problem. But without correct output with enabled ssl debug it is only my opinion :) – Hrabosch Jul 18 '16 at 12:52
  • Exceptions due to a cert problem identify the problem as a cert problem, like `ValidatorException` in the Q you link, and occur within routines in the stacktrace that are involved with cert checking, like `ClientHandshaker.serverCertificate` and `X509TrustManagerImpl.checkServerTrusted` and `PKIXValidator.engineValidate` there. – dave_thompson_085 Jul 18 '16 at 23:40
  • Yes, but for this you should have to enabled ssl debug, shouldnt you? Maybe I am wrong, but I think that this will appear only with this ssl debug enabled – Hrabosch Jul 19 '16 at 09:03
  • The exception is the same regardless of debug setting. Whether the exception is displayed with the stacktrace can depend on your code, which _could_ look at the debug setting, but this OP posted the stacktrace and clearly did not have the debug setting. – dave_thompson_085 Jul 19 '16 at 22:13