2012-04-27 12 views
2

こちらのように、私は自己署名入りの証明書を持つサイトと通信しようとしています。これを行う方法を示す多くのクラスとコードスニペットがあります(例: HTTPS GET (SSL) with Android and self-signed server certificateしかし、私はどのように実際にクラスを使用する方法を示して見つけることができません。私は多大な実験をしましたが、私は単純なものを欠いていると確信しています。Android:自己署名された証明書のエラーを無視する。実際の実装ですか?

具体的には、「Moss」によって提供されるクラスを使用しようとしています。最も票を集めた答え。私はまだ "信頼されていない証明書"メッセージを常に取得します。そのトピックの他の1人は、実装のヒントについても尋ねてきましたが、回答はありませんでした。助けてくれてありがとう。

私はその話題にこの質問を追加することができますが、私はもっと幸せですが、私のような初心者は新しい質問を投稿することができます(私は1年以上SOのファンでした)。

答えて

2

私たちはいくつかのプロジェクトで同様のSocketFactoryX509TrustManagerの実装を使用します。これらのクラスを実装にバインドするには、クライアントとサーバー間の通信に使用されるHttpClient(使用しているものと仮定します)に接続するだけです。

通常、前述のファクトリとtrustmanagerを使用してHttpClientを作成する方法があります。これはいくらか似ていて、インラインコメントに示されているリンクに基づいています。

protected HttpClient constructClient() { 
    HttpParams params = new BasicHttpParams(); 
    params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1); 

    // use the debug proxy to view internet traffic on the host computer 
    if (ABCApplication.isDebuggingProxy()) params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(ABCConstants.DEBUG_PROXY_HOST, ABCConstants.DEBUG_PROXY_PORT, "http")); 

    // ignore ssl certification (due to signed authority not appearing on android list of permitted authorities) 
    // see: http://blog.antoine.li/2010/10/22/android-trusting-ssl-certificates/ 
    SchemeRegistry registry = new SchemeRegistry(); 
    registry.register(new Scheme("http", new PlainSocketFactory(), 80)); 
    registry.register(new Scheme("https", new FakeSocketFactory(), 443)); 
    ClientConnectionManager cm = new SingleClientConnManager(params, registry); 

    return new DefaultHttpClient(cm, params); 
} 

あなたの実装に役立つ希望。

+0

を使用することができます!それは私がやった他のすべてのものがやっていたことと同じように見えますが、かなり多くのパラメータを追加していました。彼らはまた、Singleの代わりにThreadSafeClientConnManagerを使うことを好むように思えました。私は時間があるとき、私は違いについて自分自身を教育するでしょう。気高い! (もし私が担当していたら、私はあなたの答えを+1するだろう)。 – MBro

2

我々はまた、それはただそれだけで働いていた、助けにはならなかった

HttpURLConnection http = null; 
      URL url; 
      try { 
       url = new URL("https:your domian"); 

       if (url.getProtocol().toLowerCase().equals("https")) { 
        trustAllHosts(); 
        HttpsURLConnection https = (HttpsURLConnection) url.openConnection(); 
        https.setHostnameVerifier(DO_NOT_VERIFY); 
        http = https; 
        System.out.println("TEST:::"+convertStreamToString(http.getInputStream())); 
       } else { 
        http = (HttpURLConnection) url.openConnection(); 
        System.out.println("TEST:::"+convertStreamToString(http.getInputStream())); 
       } 
      } catch (MalformedURLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 


/******* 
* Trust every server - don't check for any certificate 
*/ 
private static void trustAllHosts() { 
    // Create a trust manager that does not validate certificate chains 
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return new java.security.cert.X509Certificate[] {}; 
     } 

     public void checkClientTrusted(X509Certificate[] chain, 
       String authType) throws CertificateException { 
     } 

     public void checkServerTrusted(X509Certificate[] chain, 
       String authType) throws CertificateException { 
     } 
    } }; 

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



final static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { 
    public boolean verify(String hostname, SSLSession session) { 
     return true; 
    } 
}; 
関連する問題