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
- CloseableHttpClient
- CloseableHttpClient信息
- CloseableHttpClient类的源码
- CloseableHttpClient设置Timeout
- 安卓上使用CloseableHttpClient,MultipartEntityBuilder
- CloseableHttpClient设置Timeout
- CloseableHttpClient NoSuchFieldError错误
- CloseableHttpClient 设置授权信息
- 忽略证书验证的CloseableHttpClient
- CloseableHttpClient加载证书来访问https网站
- CloseableHttpClient https上传文件与form表单
- Apache的HttpClient创建CloseableHttpClient的例子
- HttpClient之用CloseableHttpClient发送post请求
- CloseableHttpClient加载证书来访问https网站(转载)
- 使用Apache中的HttpClient的实例CloseableHttpClient的一个例子
- 使用Apache中的HttpClient的实例CloseableHttpClient进行http请求
- Java解析网络数(Json)运用CloseableHttpClient
- HttpClient 4.5.2版本设置连接超时时间-CloseableHttpClient设置Timeout
- 在WAMPSERVER下增加多版本的PHP(PHP5.4,PHP5.6)支持
- hdu 1005 矩阵快速幂
- SDUT 2408 Pick apples 贪心+背包(大容量背包)
- xoj 1172
- 第三章 -寄存器
- CloseableHttpClient
- maven配置setting.xml文件详解
- iOS app 疑问解答
- easyui.min.js 报错rows is undefined
- Hexo安装百度统计流程
- 3月21号 雨
- UIButton - 设置图片及文字、文字左对齐
- maven配置pom.xml文件详解
- 浅谈Ubuntu上离线安装包的制作与安装问题