2009-08-13 8 views
2

上のHTTPS経由でファイルをダウンロードするには、私は理由のjavaここ

  int status = client.executeMethod(get); 

は、私は次のエラーを取得する行の

/** 
* A simple example that uses HttpClient to perform a GET using Basic 
* Authentication. Can be run standalone without parameters. 
* 
* You need to have JSSE on your classpath for JDK prior to 1.4 
* 
* @author Michael Becke 
*/ 
public class BasicAuthenticationExample { 

    /** 
    * Constructor for BasicAuthenticatonExample. 
    */ 
    public BasicAuthenticationExample() { 
     super(); 
    } 

    public static void main(String[] args) throws Exception { 
     HttpClient client = new HttpClient(); 

     client.getState().setCredentials(
       new AuthScope("login.website.com", 443), 
       new UsernamePasswordCredentials("login id", "password") 
       ); 

     GetMethod get = new GetMethod(
       "https://url to the file"); 

     get.setDoAuthentication(true); 

     try { 
      // execute the GET 
      int status = client.executeMethod(get); 

      // print the status and response 
      System.out.println(status + "\n" + 
        get.getResponseBodyAsString()); 

     } finally { 
      // release any connection resources used by the method 
      get.releaseConnection(); 
     } 
    } 
} 

今ウェブからコピーしたコードである

Exception in thread "main" 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 
at com.sun.net.ssl.internal.ssl.Alerts.getSSLException(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.fatal(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.fatalSE(Unknown Source) 
at com.sun.net.ssl.internal.ssl.ClientHandshaker.serverCertificate(Unknown Source) 
at com.sun.net.ssl.internal.ssl.ClientHandshaker.processMessage(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.processLoop(Unknown Source) 
at com.sun.net.ssl.internal.ssl.Handshaker.process_record(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) 
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.writeRecord(Unknown Source) 
at com.sun.net.ssl.internal.ssl.AppOutputStream.write(Unknown Source) 
at java.io.BufferedOutputStream.flushBuffer(Unknown Source) 
at java.io.BufferedOutputStream.flush(Unknown Source) 
at org.apache.commons.httpclient.HttpConnection.flushRequestOutputStream(HttpConnection.java:828) 
at org.apache.commons.httpclient.HttpMethodBase.writeRequest(HttpMethodBase.java:2116) 
at org.apache.commons.httpclient.HttpMethodBase.execute(HttpMethodBase.java:1096) 
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:398) 
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171) 
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397) 
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:323) 
at BasicAuthenticationExample.main(BasicAuthenticationExample.java:38) 
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.validator.PKIXValidator.doBuild(Unknown Source) 
at sun.security.validator.PKIXValidator.engineValidate(Unknown Source) 
at sun.security.validator.Validator.validate(Unknown Source) 
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.validate(Unknown Source) 
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) 
at com.sun.net.ssl.internal.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source) 
... 18 more 
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target 
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(Unknown Source) 
at java.security.cert.CertPathBuilder.build(Unknown Source) 
... 24 more 

今、このエラーの原因は私のサーバーによって送信された証明書がまだ私の信頼できる証明書のリストにないことです。 私の質問は、信頼できるリストに証明書を取得する方法です。

おかげ Pranabesh

答えて

3

このページでは、あなたの質問に答えるように思われます。

信頼できるストアに証明書を実際に追加したくない場合は、テスト専用です。したがって、disable certificate validationも可能です。

+2

おかげを実行し、次のようにkeytoolは使用することができます。 –

+2

男、どんな喜ばしいフィードバック;) –

2

信頼する証明書を含むキーストアが必要です。 Javaのkeytoolコマンドを使用してこれを行うことができます。

  • が代替キーストアの場所にシステムプロパティjavax.net.ssl.trustStoreを設定します(Windows上のホームディレクトリまたはユーザーディレクトリに.keystore

    • は、デフォルトのJavaキーストアに追加します(これを行うにはいくつかの方法があります。 java -Djavax.net.ssl.trustStore=/path/to/keystoreのようなもの)
    • ストアにあなたが望む任意の方法をロードすることができ、独自のSSLSocketFactoryの実装を構築する(これは間違いなく最も複雑である)

    キーストアが単にファイルthaのですさまざまなものに使用できるキーおよび/または証明書(この場合、アプリケーションが信頼する必要がある証明書)が含まれています。他のコマンドラインオプションについて

    keytool -importcert -file /path/to/certificate/file -keystore /path/to/keystore 
    

    だけlot.Youは、おそらく私が仕事を得る助け

    keytool -help