2

AndroidでSSL接続を実現するのに苦労しています。私のプログラムは、特定の時間間隔でデータを送信します。送信データ量を抑えるために、SSL キャッシュを使用したいと考えています。自己署名証明書と SSL キャッシュを使用して Android で HttpClient を作成する方法を知っている人はいますか?

HttpClient クラスを使用したいのですが、私のソリューションは次のようになります (CastException で終了します)。

private HttpClient getNewHttpClient(Context context, InputStream sslCertificate) {
        SSLSessionCache sslSessionCache = new SSLSessionCache(context);
        SSLCertificateSocketFactory socketFactory = (SSLCertificateSocketFactory) SSLCertificateSocketFactory.getDefault(15000, sslSessionCache);

        KeyStore keyStore = getSSLKeyStore(sslCertificate);           
        TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
        tmf.init(keyStore);

        socketFactory.setTrustManagers(tmf.getTrustManagers());                   

        HttpParams connectionParameters = new BasicHttpParams();
        HttpProtocolParams.setVersion(connectionParameters , HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(connectionParameters , HTTP.UTF_8);
        HttpConnectionParams.setStaleCheckingEnabled(connectionParameters , false);
        HttpConnectionParams.setConnectionTimeout(connectionParameters , 4 * 1000);
        HttpConnectionParams.setSoTimeout(connectionParameters , 5 * 1000);
        HttpConnectionParams.setSocketBufferSize(connectionParameters , 8192);
        HttpClientParams.setRedirecting(connectionParameters , false);

        SchemeRegistry registry = new SchemeRegistry();     

        registry.register(new Scheme("https",  (SocketFactory) socketFactory , 443));
        ClientConnectionManager connectionManager = new ThreadSafeClientConnManager(connectionParameters, registry);

        HttpClient httpClient = new DefaultHttpClient(connectionManager, connectionParameters);

        return httpClient;
}

これは、新しいビルド スキームを SchemeRegistry に登録する際の CastException です。

 W/System.err(343): java.lang.ClassCastException: org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl cannot be cast to org.apache.http.conn.scheme.SocketFactory
4

1 に答える 1

-1
    public class SimpleSSLSocketFactory extends
    org.apache.http.conn.ssl.SSLSocketFactory {
private javax.net.ssl.SSLSocketFactory sslFactory = HttpsURLConnection
        .getDefaultSSLSocketFactory();

public SimpleSSLSocketFactory(KeyStore truststore)
        throws NoSuchAlgorithmException, KeyManagementException,
        KeyStoreException, UnrecoverableKeyException {
    super(null);
    try {
        SSLContext context = SSLContext.getInstance("TLS");
        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 {
            }
        } };

        // Initialize the socket factory
        context.init(null, trustAllCerts, new SecureRandom());
        sslFactory = context.getSocketFactory();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

@Override
public Socket createSocket(Socket socket, String host, int port,
        boolean autoClose) throws IOException, UnknownHostException {
    return sslFactory.createSocket(socket, host, port, autoClose);
}

@Override
public Socket createSocket() throws IOException {
    return sslFactory.createSocket();
}

}

   U can call like this 


                  try {
        SSLSocketFactory sslFactory = new SimpleSSLSocketFactory(null);
        sslFactory
                .setHostnameVerifier(SSLSocketFactory.
                    ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory
                .getSocketFactory(), 80));
        registry.register(new Scheme("https", sslFactory, 443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                params, registry);
        DefaultHttpClient httpClient = new DefaultHttpClient(ccm, params);
        HttpPost httpPost = new HttpPost(url);
        HttpHost proxy = new HttpHost("50.104.5.277", 23333);

        //httpPost.setHeader("Content-Type", "text/xml");
        httpPost.setEntity(entity);
        httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                proxy);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        output = EntityUtils.toString(httpEntity);

        // Toast.makeText(getApplicationContext(), xml, 1000).show();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (KeyManagementException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnrecoverableKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (KeyStoreException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return output;
}
于 2013-09-06T14:25:03.840 に答える