apache httpclient 4.5 兼容 http https

 1 String responseContent = "";
 2 try {
 3     SSLContextBuilder contextBuilder = new SSLContextBuilder();
 4     contextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
 5     SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(contextBuilder.build());
 6     CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
 7     HttpGet httpGet = new HttpGet(requestUrl);
 8     CloseableHttpResponse response = httpclient.execute(httpGet);
 9     try {
10         HttpEntity entity = response.getEntity();
11         if (null != entity) {
12             responseContent = EntityUtils.toString(entity, ContentType.getOrDefault(entity).getCharset());
13             EntityUtils.consume(entity);
14         }
15     } finally {
16         response.close();
17     }
18 } catch (KeyStoreException e) {
19     e.printStackTrace();
20 } catch (NoSuchAlgorithmException e) {
21     e.printStackTrace();
22 } catch (KeyManagementException e) {
23     e.printStackTrace();
24 }
25 return responseContent;

 

posted @ 2016-04-05 15:35  Zombie_Xian  阅读(937)  评论(0编辑  收藏  举报