Java以httpcomponents-client-4.3.x library建構可用的SSL Client連線

之前的HttpClient一直都是寫成

HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
try{
    TrustManager easyTrustManager = new X509TrustManager() {
        public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }
        public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
            //To change body of implemented methods use File | Settings | File Templates.
        }
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            // return new java.security.cert.X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
            return null;
        }
    };
    SSLContext sslcontext = SSLContext.getInstance("TLS");
    sslcontext.init(null, new TrustManager[]{easyTrustManager}, null);
    SSLSocketFactory socketFactory = new SSLSocketFactory(sslcontext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    httpclient.getConnectionManager().getSchemeRegistry().register( new Scheme("https", 443, socketFactory) );
    httpclient.getConnectionManager().getSchemeRegistry().register( new Scheme("http", 80, PlainSocketFactory.getSocketFactory()) );
} catch (NoSuchAlgorithmException e) {
	e.printStackTrace();
} catch (KeyManagementException e) {
	e.printStackTrace();
}

自從Apache的HttpComponents Client4.3模組,許多函式庫都已經deprecated了。
今天趁著心情好,將整個改了一下:

try{
	TrustManager easyTrustManager = new X509TrustManager() {
	    public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
	        //To change body of implemented methods use File | Settings | File Templates.
	    }
	    public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates, String s) throws java.security.cert.CertificateException {
	        //To change body of implemented methods use File | Settings | File Templates.
	    }
	    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
	        // return new java.security.cert.X509Certificate[0];  //To change body of implemented methods use File | Settings | File Templates.
	        return null;
	    }
	};
	SSLContext sslcontext = SSLContext.getInstance("TLS");
	sslcontext.init(null, new TrustManager[]{easyTrustManager}, null);
      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory( sslcontext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
      this.httpclient = HttpClients.custom().setSSLSocketFactory( sslsf ).build();
} catch (NoSuchAlgorithmException e) {
	e.printStackTrace();
} catch (KeyManagementException e) {
	e.printStackTrace();
}
廣告

About fenjj

Perfect !!??...
本篇發表於 Uncategorized。將永久鏈結加入書籤。

發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com Logo

您的留言將使用 WordPress.com 帳號。 登出 / 變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 / 變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 / 變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 / 變更 )

連結到 %s