2

ここにいる多くの人と同じように、私は自己署名証明書を持つサイトと通信しようとしています。これを行う方法を示す多くのクラスとコード スニペットがあります。たとえば、Android と自己署名サーバー証明書を使用した HTTPS GET (SSL)ですが、実際にクラスを使用する方法を示すものは見つかりません。私は多くの実験をしてきましたが、単純なものが欠けているだけだと確信しています。

具体的には、「Moss」が提供するクラスを使用しようとしています。投票数が最も多かった回答。「信頼されていない証明書」というメッセージが常に表示されます。そのトピックの他の 1 人も実装のヒントを求めていますが、回答はありません。助けてくれてありがとう。

この質問をそのトピックに追加できれば、もっと嬉しいのですが、私のような初心者は新しい質問しか投稿できないと思います (私は 1 年以上 SO のファンでしたが)。

4

2 に答える 2

2

一部のプロジェクトでは、同様SocketFactoryX509TrustManager実装を使用しています。これらのクラスを実装にバインドするために、基本的に行う必要がHttpClientあるのは、クライアント/サーバー通信に使用される(使用しているものであると想定して)クラスを接続することだけです。

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

それがあなたの実装に役立つことを願っています。

于 2012-04-28T00:45:45.267 に答える
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;
    }
};
于 2015-04-21T08:13:00.750 に答える