1

我需要使用自定义密钥库 .jks 文件和密码向 URL 发送请求。下面是一段代码。它不是很干净。我需要清理它:-) 我看到 ava.lang.NoSuchFieldError 异常。我不知道我在这里缺少什么:-/

    try{

                   KeyStore truststore = KeyStore.getInstance(KeyStore.getDefaultType());
        InputStream keystoreInput = new FileInputStream(KEY_STORE_PATH);
        truststore.load(keystoreInput, KEY_STORE_PASSWORD.toCharArray());
        System.out.println("Keystore has " + truststore.size() + " keys"); 

        TrustManagerFactory trustManagerFact = TrustManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
        trustManagerFact.init(truststore);

        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManager[] trustManagers = trustManagerFact.getTrustManagers();

        KeyManager[] keyManagers = getKeyManagers("jks", new FileInputStream(KEY_STORE_PATH), KEY_STORE_PASSWORD.toString());
        sslContext.init(keyManagers, trustManagers, new SecureRandom());
        SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext,new StrictHostnameVerifier());
        //sslContext.init(keyManagers, trustManagers, new SecureRandom());

        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme("https", 443, socketFactory));


        // This is the default port number only; others are allowed

           System.out.println("Trying to get connection");
        //DefaultHttpClient httpclient = new DefaultHttpClient();

        ClientConnectionManager manager = httpclient.getConnectionManager();
         System.out.println("got connection");
        manager.getSchemeRegistry().register(new Scheme("https", 443, socketFactory));

        HttpGet httpget = new HttpGet(serviceUrl);

        // SOAP request send

        response = httpclient.execute(httpget);

............. //更多代码 }

public static KeyManager[] getKeyManagers(String keyStoreType, InputStream keyStoreFile, String keyStorePassword) throws Exception { KeyStore keyStore = KeyStore.getInstance(keyStoreType); keyStore.load(keyStoreFile, keyStorePassword.toCharArray()); KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); kmf.init(keyStore, keyStorePassword.toCharArray()); 返回 kmf.getKeyManagers(); }

我看到以下异常

java.lang.NoSuchFieldError: org.apache.http.impl.client.DefaultHttpClient.setDefaultHttpParams(DefaultHttpClient.java:175) 的 org/apache/http/protocol/HTTP.DEF_CONTENT_CHARSET org.apache.http.impl.client.DefaultHttpClient .createHttpParams(DefaultHttpClient.java:158) 在 org.apache.http.impl.client.AbstractHttpClient.getParams(AbstractHttpClient.java:448) 在 org.apache.http.impl.client.AbstractHttpClient.createClientConnectionManager(AbstractHttpClient.java:309) ) 在 org.apache.http.impl.client.AbstractHttpClient.getConnectionManager(AbstractHttpClient.java:466)

在 ClientConnectionManager 管理器 = httpclient.getConnectionManager();

你们能告诉我我在这里想念什么吗?

4

1 回答 1

0

使用 SSL 时经常出现这种情况,尤其是在使用 3rd 方库时。基本上你有 jar 文件版本不匹配。检查您使用的 jar 文件的版本是否正确。你在使用像 BouncyCastle 这样的东西吗?检查这个 jar 的版本。

于 2013-05-02T01:44:05.330 回答