CloseableHttpClient

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();
            }
        }
    }
}
  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值