2014-02-19 65 views
0

我有以下https客戶端,但其中包含已棄用的方法。當我嘗試刪除它們時,我沒有收到回覆。 你可以幫我得到這個客戶端沒有不推薦的方法。爲Apache Camel創建http客戶端以啓用證書啓用web服務

棄用方法: SSLSocketFactory的 SchemeRegistry 方案 getConnectionManager()

我的客戶:

HttpComponent httpComponent = context.getComponent( 「https4」,HttpComponent.class);

KeyStore keystore = KeyStore.getInstance("JKS"); 
    keystore.load(new FileInputStream(new File("XXXXXX")), "XXXXXX".toCharArray()); 
    //keystore.load(new FileInputStream(new File("C:/certificate/ToPankaj/clientcert.jks")), "changeit".toCharArray()); 

    KeyManagerFactory keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); 
    keyFactory.init(keystore, "changeit".toCharArray()); 


    SSLContext sslcontext = SSLContext.getInstance("SSLv3"); 
    sslcontext.init(keyFactory.getKeyManagers(), null, null); 


    @SuppressWarnings("deprecation") 
    SSLSocketFactory factory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); 

    @SuppressWarnings("deprecation") 
    SchemeRegistry registry = new SchemeRegistry(); 

    @SuppressWarnings("deprecation") 
    final Scheme scheme = new Scheme("https4", 443, factory); 

    //registry.register(scheme); 

    httpComponent.setHttpClientConfigurer(new HttpClientConfigurer() 
    { 

     @SuppressWarnings("deprcecation") 
     @Override 
     public void configureHttpClient(HttpClient client) { 
      client.getConnectionManager().getSchemeRegistry().register(scheme); 
     } 
    }); 

    httpComponent.setClientConnectionManager(new ThreadSafeClientConnManager()); 
    //httpComponent. 
    Endpoint endpoint =httpComponent.createEndpoint(Uri); 

    return endpoint; 

回答

1

使用從Camel doc的例子:

import org.apache.camel.builder.RouteBuilder; 
import org.apache.camel.component.http.SSLContextParametersSecureProtocolSocketFactory; 
import org.apache.camel.main.Main; 
import org.apache.camel.util.jsse.KeyManagersParameters; 
import org.apache.camel.util.jsse.KeyStoreParameters; 
import org.apache.camel.util.jsse.SSLContextParameters; 
import org.apache.commons.httpclient.protocol.Protocol; 
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory; 

public class SSLHttpClientRouteBuilder extends RouteBuilder { 

    @Override 
    public void configure() { 
     final KeyStoreParameters ksp = new KeyStoreParameters(); 
     ksp.setResource("https://stackoverflow.com/users/home/server/keystore.jks"); 
     ksp.setPassword("keystorePassword"); 

     final KeyManagersParameters kmp = new KeyManagersParameters(); 
     kmp.setKeyStore(ksp); 
     kmp.setKeyPassword("keyPassword"); 

     final SSLContextParameters scp = new SSLContextParameters(); 
     scp.setKeyManagers(kmp); 

     final ProtocolSocketFactory factory = new SSLContextParametersSecureProtocolSocketFactory(scp); 

     Protocol.registerProtocol("https", new Protocol("https", factory, 443)); 

     from("direct:start").to("https://mail.google.com/mail/").to("mock:results"); 
    } 
} 

Alternativelly,你也可以直接使用Apache HTTP客戶端作爲Camel doc

+0

提到其實我看到這個例子....這示例不會創建https客戶端 – bhalkian

+1

'to(「https://mail.google.com/mail/」)'定義客戶端從服務器讀取數據,不是嗎? –

+0

它的確如此。另外,這是我見過的最簡單的例子,因爲它並不要求你自己擺弄HTTP客戶端。只需添加密鑰庫或信任庫就可以滿足許多用例。 – Jaap

相關問題