2016-08-02 18 views
0

私は2つのクラスを作成しました: - この1つはOkHttpを拡張し、新しいメソッドgetUnsafeOkHttpClient()を持っています。ssl証明書でokhttpを作成する方法

public class GetExampleOkHttp extends OkHttpClient { 
    public OkHttpClient getUnsafeOkHttpClient() { 
     try { 
      // Create a trust manager that does not validate certificate chains 
      final TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
       @Override 
       public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) 
         throws CertificateException { 
       } 
       @Override 
       public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) 
         throws CertificateException { 
       } 
       @Override 
       public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
        return new java.security.cert.X509Certificate[] {}; 
       } 
      } }; 
      // Install the all-trusting trust manager 
      final SSLContext sslContext = SSLContext.getInstance("SSL"); 
      sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); 
      // Create an ssl socket factory with our all-trusting manager 
      final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); 
      OkHttpClient.Builder builder = new OkHttpClient.Builder(); 
      builder.sslSocketFactory(sslSocketFactory); 
      builder.hostnameVerifier(new HostnameVerifier() { 
       @Override 
       public boolean verify(String hostname, SSLSession session) { 
        return true; 
       } 
      }); 
      OkHttpClient okHttpClient = builder.build(); 
      return okHttpClient; 
     } catch (Exception e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 

そして、他のメインクラス:

class ExampleOkHttpMain 
    public static void main(String[] args) throws IOException {  
     GetExampleOkHttp example = new GetExampleOkHttp();   
     example.getUnsafeOkHttpClient(); 

      Request request = new Request.Builder() 
       .url("https://lachuzhnikov.kiev.ua/test.txt") 
       .build(); 
      Response response = example.newCall(request).execute(); 
      System.out.println(response.body().string());  
    } 
} 

しかし、私はまだエラーを持っている:

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 sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source)

答えて

0

1:https://sourceforge.net/projects/portecle/
2ダウンロード:Firefoxを起動し、ページと輸出CERに行きます
バックアップJava¥jdk ....¥jre¥lib¥security¥cacerts(例:C:¥Program Files¥Java¥jdk1.7.0_79¥jre¥lib¥security)
4:cacertsを別のフォルダにコピーします。
5:ポケットベルを開いて、手順2でcacerts(pass:changeit)をインポートして
を追加します。 6:新しいcacertsをコピーして置き換えます。Java¥jdk ....¥jre¥lib¥セキュリティ\ cacerts