HttpClient忽略证书访问HTTPS接口

来源:互联网 发布:京东运营和淘宝运营 编辑:程序博客网 时间:2024/05/16 17:06

HttpClient 如何忽略证书验证访问HTTPS接口,解决步骤如下:

1. 设置可以访问HTTPS

Java代码  收藏代码
  1. /**  
  2.     * @Title: getNewHttpClient  
  3.     * @Description: Methods Description 
  4.     * @param @return     
  5.     * @return HttpClient  
  6.     * @throws  
  7.     */   
  8.       
  9.     private HttpClient getNewHttpClient() {  
  10.         try {  
  11.             KeyStore trustStore = KeyStore.getInstance(KeyStore  
  12.                     .getDefaultType());  
  13.             trustStore.load(nullnull);  
  14.             SSLSocketFactory sf = new SSLSocketFactory(trustStore);  
  15.             sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);  
  16.             HttpParams params = new BasicHttpParams();  
  17.             HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);  
  18.             HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);  
  19.             SchemeRegistry registry = new SchemeRegistry();  
  20.             registry.register(new Scheme(“http”, PlainSocketFactory  
  21.                     .getSocketFactory(), 80));  
  22.             registry.register(new Scheme(“https”, sf, 443));  
  23.             ClientConnectionManager ccm = new ThreadSafeClientConnManager(  
  24.                     params, registry);  
  25.             return new DefaultHttpClient(ccm, params);  
  26.         } catch (Exception e) {  
  27.             return new DefaultHttpClient();  
  28.         }  
  29.     }  

 

2. 忽略证书验证,使用HttpClient访问HTTPS接口

Java代码  收藏代码
  1. DefaultHttpClient httpclient = (DefaultHttpClient) getNewHttpClient();  
  2.                   
  3.         try {  
  4.             //Secure Protocol implementation.    
  5.             SSLContext ctx = SSLContext.getInstance(”SSL”);  
  6.             //Implementation of a trust manager for X509 certificates    
  7.             X509TrustManager tm = new X509TrustManager() {  
  8.   
  9.                 public void checkClientTrusted(X509Certificate[] xcs,  
  10.                         String string) throws CertificateException {  
  11.   
  12.                 }  
  13.   
  14.                 public void checkServerTrusted(X509Certificate[] xcs,  
  15.                         String string) throws CertificateException {  
  16.                 }  
  17.   
  18.                 public X509Certificate[] getAcceptedIssuers() {  
  19.                     return null;  
  20.                 }  
  21.             };  
  22.             ctx.init(nullnew TrustManager[] { tm }, null);  
  23.             SSLSocketFactory ssf = new SSLSocketFactory(ctx);  
  24.             ClientConnectionManager ccm = httpclient.getConnectionManager();  
  25.             //register https protocol in httpclient’s scheme registry    
  26.             SchemeRegistry sr = ccm.getSchemeRegistry();  
  27.             sr.register(new Scheme(“https”443, ssf));  
  28.         } catch (Exception e) {  
  29.             e.printStackTrace();  
  30.         }  
  31.   
  32.         HttpGet httpGet = new HttpGet(httpGetUrl);  
  33.         HttpResponse response = httpclient.execute(httpGet, localContext);  
 
0 0
原创粉丝点击