CloseableHttpClient

来源:互联网 发布:淘宝女装代发货源 编辑:程序博客网 时间:2024/06/06 00:34

1.最新版的httpClient使用实现类的是closeableHTTPClient,以前的default作废了. 自4.3版本之后出现的。

2.之前的httpClient 不能使用SSL证书验证。(还未具体的验证,不过之前的方法确实是没找到使用方法)。

在研究最新的微信支付的时候,发现微信退款接口需要双向证书验证。然后研究微信支付的DEMO,发现使用的是4.3.2的httpClient版本。里面使用的就是CloseableHttpClient。代码如下:

/**    * 需要加密执行的    * @param url    * @param xmlInfo    * @return    * @throws Exception     */   public static String postHttplientNeedSSL(String url,                                             String xmlInfo,                                             String cretPath,                                             String mrchId)         throws Exception{      //选择初始化密钥文件格式      KeyStore keyStore = KeyStore.getInstance("PKCS12");      //得到密钥文件流      FileInputStream instream = new FileInputStream(new File(cretPath));      try{         //用商户的ID 来解读文件         keyStore.load(instream, mrchId.toCharArray());      }finally{         instream.close();      }      //用商户的ID 来加载      SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, mrchId.toCharArray()).build();      // Allow TLSv1 protocol only      SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);      //用最新的httpclient 加载密钥      CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();      StringBuffer ret = new StringBuffer();      try{         HttpPost httpPost = new HttpPost(url);         httpPost.setEntity(new StringEntity(xmlInfo));         CloseableHttpResponse response = httpclient.execute(httpPost);         try{            HttpEntity entity = response.getEntity();            if(entity != null){               BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));               String text;               while((text = bufferedReader.readLine()) != null){                  ret.append(text);               }            }            EntityUtils.consume(entity);         }finally{            response.close();         }      }finally{         httpclient.close();      }      return ret.toString();   }

3 HttpClientBuilder

HttpClientBuilder用于创建CloseableHttpClient实例。看了一下API文档,AbstractHttpClient、 AutoRetryHttpClient、 DefaultHttpClient等都被弃用了,使用HttpClientBuilder代替。

public class HttpClientTest {    public static void main(String args[]) {        //创建HttpClientBuilder        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();        //HttpClient        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();        HttpGet httpGet = new HttpGet("http://www.gxnu.edu.cn/default.html");        System.out.println(httpGet.getRequestLine());        try {            //执行get请求            HttpResponse httpResponse = closeableHttpClient.execute(httpGet);            //获取响应消息实体            HttpEntity entity = httpResponse.getEntity();            //响应状态            System.out.println("status:" + httpResponse.getStatusLine());            //判断响应实体是否为空            if (entity != null) {                System.out.println("contentEncoding:" + entity.getContentEncoding());                System.out.println("response content:" + EntityUtils.toString(entity));            }        } catch (IOException e) {            e.printStackTrace();        } finally {            try {                //关闭流并释放资源                closeableHttpClient.close();            } catch (IOException e) {                e.printStackTrace();            }        }    }}
0 0
原创粉丝点击