0

I am trying to get contents of a site using java. like this

    package javaTests;

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.URL;

    import javax.net.ssl.HttpsURLConnection;

public class PrintContent {
    public static void main(String[] args) throws Exception {
        String https_url = "https://api.precharge.net/charge";
        URL url;
        url = new URL(https_url);
        HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

        if(con!=null){
        try {

           System.out.println("****** Content of the URL ********");            
           BufferedReader br = 
            new BufferedReader(
                new InputStreamReader(con.getInputStream()));

           String input;

           while ((input = br.readLine()) != null){
              System.out.println(input);
           }
           br.close();

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

             }


    }
}

But i am getting exception like.

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

Please give some suggestions to how to fix this. thank you in advace.

i tried copying the certificate to the security folder of java also. but it is not working. also tried installcerticate.java class.

Raghavendra
  • 3,530
  • 1
  • 17
  • 18
  • I don't want to mark it as duplicate, because I don't know a lot about the subject, but have you looked at: http://stackoverflow.com/questions/9210514/unable-to-find-valid-certification-path-to-requested-target-error-even-after-c – Tom Jonckheere Jul 06 '15 at 07:08
  • @TomJonckheere. i am not sure how to use that. and i am writing it as a standlone class. not in server. not sure if this helps – Raghavendra Jul 06 '15 at 07:12
  • 1
    Ok, and does the link in the linked question help? http://stackoverflow.com/questions/4062307/pkix-path-building-failed-unable-to-find-valid-certification-path-to-requested – Tom Jonckheere Jul 06 '15 at 07:19
  • Hi @raghavendra I executed the same program, but I didn't get any exception as you mentioned above.I am getting the unknown host exception for the code.So I placed the google.com URL, instead of your URL. And the code is working fine.It has given me the html content. – bhadram Jul 06 '15 at 07:21
  • @bhadram for google it is working in my system also. – Raghavendra Jul 06 '15 at 08:17
  • but for me I am getting different exception like I mentioned above. – bhadram Jul 06 '15 at 08:46

1 Answers1

0

If you have failed to install the certificate in the truststore you could use this trick to disable the SSL validation. Should not be used in a production environment.

package javaTests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.security.SecureRandom;

public class PrintContent {
  public static void main(String[] args) throws Exception {
    String https_url = "https://api.precharge.net/charge";
    URL url;
    url = new URL(https_url);

    HttpsURLConnection con = (HttpsURLConnection) url.openConnection();

    if (con != null) {
      try {

        System.out.println("****** Content of the URL ********");
        BufferedReader br =
            new BufferedReader(
                new InputStreamReader(con.getInputStream()));

        String input;

        while ((input = br.readLine()) != null) {
          System.out.println(input);
        }
        br.close();

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

  public void disableCertificates() {
    TrustManager[] trustAllCerts = new TrustManager[] {
        new X509TrustManager() {
          @Override
          public X509Certificate[] getAcceptedIssuers() {
            System.out.println("Warning! SSL validation has been disabled!");
            return null;
          }

          @Override
          public void checkServerTrusted(X509Certificate[] certs, String authType) {
            System.out.println("Warning! SSL validation has been disabled!");
          }

          @Override
          public void checkClientTrusted(X509Certificate[] certs, String authType) {
            System.out.println("Warning! SSL validation has been disabled!");
          }
        }
    };
    try {
      SSLContext sc = SSLContext.getInstance("SSL");
      sc.init(null, trustAllCerts, new SecureRandom());
      HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    } catch (Exception e) {
      System.out.println("Failed to disable SSL validation," + e.getMessage());
    }
  }
}
aksamit
  • 2,325
  • 8
  • 28
  • 40