Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle https url that ends up plaintext connection

Tags:

java

https

I try to get the page content of a https url that throws an exception while getting input stream.

String httpsURL = "https://careers.virtusa.com/";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
InputStream ins = con.getInputStream();

The exception is as below,

Exception in thread "main" javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?
    at com.sun.net.ssl.internal.ssl.InputRecord.handleUnknownRecord(InputRecord.java:523)
    at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:355)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:798)
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1138)    
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1165)    
    at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1149)    
    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:1172)    
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)      
    at url.JavaHttpsExample.main(JavaHttpsExample.java:18)    

Both HttpURLConnection and HttpsURLConnection fail. I tried org.apache.http.impl.client.CloseableHttpClient but getting the same exception. In browser it works fine.

like image 311
itsraja Avatar asked Apr 27 '16 14:04

itsraja


People also ask

What is unrecognized SSL message plaintext connection?

The unrecognized ssl message plaintext connection error happens due to an established connection with the client machine that is not secure. You can use numerous methods to debug this error from your syntax, and this article features it all.

How do I pass a certificate in HTTP request?

You need to send the client certificate during the TLS handshake before anything HTTP (methods, headers, URLs, request bodies) is available to be influenced. The server will not accept a client certificate sent later.

Can a password be stored as plaintext If HTTP is disabled?

If HTTP is disabled, and you only use HTTPS, then you're not really transmitting the password as plain text anyway. However the server does have access to your plaintext password, they can store it as plaintext, log it incorrectly as plaintext etc.

What is the unrecognized SSL message plaintext connection error?

The unrecognized ssl message plaintext connection error happens due to an established connection with the client machine that is not secure. This article has provided numerous methods you can use to fix the issue, but it also covered the following important notes: There are a couple of common reasons why this error appears in your Java program.

What happens if HTTP is disabled when using HTTPS?

If HTTP is disabled, and you only use HTTPS, then you're not really transmitting the password as plain text anyway. Show activity on this post. Hash client side. Why? Let me tell you about a little experiment.


1 Answers

Specify SSL version that careers.virtusa.com is using. i.e. TLSv1.2 in the code.

SSLContext sc = SSLContext.getInstance("TLSv1.2");

public String getData(String URL)
{
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }

        public void checkClientTrusted(X509Certificate[] certs,
                String authType) {
        }

        public void checkServerTrusted(X509Certificate[] certs,
                String authType) {
        }

    } };

    String output = "";
    try{

        //System.setProperty("https.proxyHost", "<PROXY HOST IP>");   // Uncomment if using proxy
        //System.setProperty("https.proxyPort", "<PROXY HOST PORT>");        // Uncomment if using proxy
        SSLContext sc = SSLContext.getInstance("TLSv1.2");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        /*
         * end of the fix
         */

        URL url = new URL(URL);
        URLConnection con = url.openConnection();

        InputStream ins = con.getInputStream();
        InputStreamReader isr = new InputStreamReader(ins);
        BufferedReader in = new BufferedReader(isr);

        String inputLine;


        while ((inputLine = in.readLine()) != null) {
            output = output + inputLine;
        }

        System.out.println(output);
        in.close();

    }   
    catch(Exception e){
        e.printStackTrace();            
    }


    return output;
}
like image 90
Kunal Surana Avatar answered Oct 14 '22 13:10

Kunal Surana