Wednesday, September 25, 2013

How to download content from HTTPS (Secured) website using Java

I spent lot of time to accomplish downloading a content from HTTPS(secured) website, I couldn't find right place to get enough information to resolve certificate related errors/exceptions while trying to connecting to https website, I thought of documenting all my findings so that others no need to spend time on researching root cause for certificates related information


       

import java.io.BufferedInputStream;
import java.io.FileOutputStream;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;


public class HTTPSFileDownload {
    public HTTPSFileDownload() {
        super();
    }

    /**
     *  This method trust all the certificates.
     */
    private void trustAllCertificates() {

        //Manager to trust all certificates
        TrustManager[] trustAllCerts =
            new TrustManager[] { new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(java.security.cert.X509Certificate[] certs,
                                               String authType) {
                }

                public void checkServerTrusted(java.security.cert.X509Certificate[] certs,
                                               String authType) {
                }
            } };

        // Activate the new trust manager
        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        HTTPSFileDownload s = new HTTPSFileDownload();
        s.trustAllCertificates();
        String https_url = "https://google.com";
        URL url;
        try{
            url = new URL(https_url);
            HttpsURLConnection con = (HttpsURLConnection)url.openConnection();
            BufferedInputStream in = null;
            FileOutputStream fout = null;
            if (con != null) {
                in = new BufferedInputStream(con.getInputStream());
                fout = new FileOutputStream("C:/RKP/httpsFile.csv");

                byte data[] = new byte[1024];
                int count;
                while ((count = in.read(data, 0, 1024)) != -1) {
                    fout.write(data, 0, count);
                }
            }
            
        }catch(Exception ex){
            ex.printStackTrace();
        }
    }
}
       
 

I hope this would helpful


No comments:

Post a Comment