2015-07-06 15 views
0

我想獲得使用java的網站的內容。像這樣我想要獲取使用java的網站的內容。但得到pxip錯誤

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(); 
     } 

      } 


    } 
} 

但我收到異常。

javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路徑建設失敗:sun.security.provider.certpath.SunCertPathBuilderException:無法找到有效的認證路徑要求的目標

請給一些建議如何解決這個問題。謝謝你advace。

我試着將證書複製到java的安全文件夾中。但它不起作用。 也試過installcerticate.java類。

+0

我不想把它標記爲重複的,因爲我不知道很多關於這個主題,但你看看:http://stackoverflow.com/questions/9210514/unable-to -find-valid-certification-path-to-requested-target-error-even-after-c –

+0

@TomJonckheere。我不知道如何使用它。我將它寫成一個獨立的課程。不在服務器中。不知道這是否有幫助 – Raghavendra

+1

好的,鏈接問題中的鏈接是否有幫助? http://stackoverflow.com/questions/4062307/pkix-path-building-failed-unable-to-find-valid-certification-path-to-requested –

回答

0

如果您未能在truststore中安裝證書,則可以使用此技巧來禁用SSL驗證。不應該在生產環境中使用。

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()); 
    } 
    } 
} 
+0

我如何在我的系統中安裝cer證書 – Raghavendra

相關問題