一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。

import com.qunar.payment.gateway.front.channel.mpgs.po.HttpReqEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpMethod;import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;/*** User:xfyou Date:2017/11/23 10:50*/
public class HttpUtil {private HttpUtil() {}private static CredentialsProvider credentialsProvider;private static final SSLConnectionSocketFactory SOCKET_FACTORY = getSocketFactory();private static final NoopHostnameVerifier NO_OP = new NoopHostnameVerifier();public static String execute(HttpReqEntity httpReqEntity) throws IOException {CloseableHttpClient httpclient = getClosableHttpClient(httpReqEntity);CloseableHttpResponse response = null;try {response = httpclient.execute(getHttpUriRequest(httpReqEntity));return EntityUtils.toString(response.getEntity());} finally {try {if (null != response) response.close();if (null != httpclient) httpclient.close();} catch (IOException ignored) {}}}private static TrustManager getTrustManagers() {return new X509TrustManager() {public X509Certificate[] getAcceptedIssuers() {return null;}public void checkClientTrusted(X509Certificate[] certs, String authType) {}public void checkServerTrusted(X509Certificate[] certs, String authType) {}};}private static SSLConnectionSocketFactory getSocketFactory() {SSLContext sslContext;try {sslContext = SSLContext.getInstance(MpgsConstant.SECURITYPROTOCOL_VERSION_TLS_1_2);} catch (NoSuchAlgorithmException e) {return null;}try {sslContext.init(null, new TrustManager[]{getTrustManagers()}, new SecureRandom());} catch (KeyManagementException e) {return null;}return new SSLConnectionSocketFactory(sslContext);}private static CloseableHttpClient getClosableHttpClient(HttpReqEntity entity) {return HttpClients.custom().setSSLSocketFactory(SOCKET_FACTORY).setSSLHostnameVerifier(NO_OP).setDefaultCredentialsProvider(getCredentialsProvider(entity.getCredUserName(), entity.getCredPasswd())).build();}private static HttpPut getHttpPut(String requestUrl, String requestMessage, RequestConfig config) {HttpPut httpPut = new HttpPut(requestUrl);httpPut.setConfig(config);httpPut.addHeader(HttpHeaders.CONTENT_TYPE, MpgsConstant.CONTENTTYPE_JSON);httpPut.setEntity(new StringEntity(requestMessage, MpgsConstant.CHARSET_UTF8));return httpPut;}private static HttpGet getHttpGet(String requestUrl, RequestConfig config) {HttpGet httpGet = new HttpGet(requestUrl);httpGet.setConfig(config);return httpGet;}private static HttpUriRequest getHttpUriRequest(HttpReqEntity entity) {return entity.getHttpMethod() == HttpMethod.GET ? getHttpGet(entity.getRequestUrl(), entity.getRequestConfig()): getHttpPut(entity.getRequestUrl(), entity.getRequestMessage(), entity.getRequestConfig());}private static CredentialsProvider getCredentialsProvider(String userName, String passwd) {if (null == credentialsProvider) {synchronized (HttpUtil.class) {if (null == credentialsProvider) {CredentialsProvider credsProvider = new BasicCredentialsProvider();credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(userName, passwd));credentialsProvider = credsProvider;}}}return credentialsProvider;}
}

一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。相关推荐

  1. Apache HttpClient连接池泄露问题排查

    Apache HttpClient连接池泄露问题排查 问题背景 业务系统主要的业务是一个数据聚合管理平台,其中系统有一个功能是同步所有资源(简称 大同步) 业务同步数据请求数据工具是适配 Apache ...

  2. 新旧apache HttpClient 获取httpClient方法

    在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...

  3. okhttp3测试框架_easy-okhttp: 这是一个对okhttp3进行封装的工具,提供了更为便捷的方法调用。目的是为了替换难用的apache HttpClient。...

    easy-okhttp 简介 项目easy-okhttp是对okhttp网络框架(https://github.com/square/okhttp)上层封装, 支持文件上传和下载,表单(含文件)提交, ...

  4. Apache HttpClient 5 笔记: SSL, Proxy 和 Multipart Upload

    Apache HttpClient 5 最近要在非SpringBoot环境调用OpenFeign接口, 需要用到httpclient, 注意到现在 HttpClient 版本已经到 5.2.1 了. ...

  5. 用Apache HttpClient实现URL重定向

    分享一下我老师大神的人工智能教程.零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow <用Apac ...

  6. Apache HttpClient 4 3开发指南

    <Apache HttpClient 4.3开发指南> 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs 一.概述 Apache HttpCli ...

  7. Apache HttpClient

    文章目录 Apache HttpClient 简介 Apache HttpClient特性 使用流程 使用实例 -END- Apache HttpClient 简介 HttpClient 是 Apac ...

  8. android httpclient gradle,Apache HttpClient Android(Gradle)

    Apache HttpClient Android(Gradle) 我已经将此行添加到我的build.gradle compile group: 'org.apache.httpcomponents' ...

  9. Linux脚本自动安装软件,一个快速自动安装Apache及其相关软件的Shell脚本

    一个快速自动安装Apache及其相关软件的Shell脚本 文:王文俊 email:wwj99001@sina.com wwj99001@sohu.com 成文时间:2002年6月1日 Apache是一 ...

最新文章

  1. php在函数使用正则表达式,php正则表达式以及正则函数的使用
  2. python学习-Django (3)
  3. 选错导师误终身?Nature大调查显示 :全球1/4博士生想换导师
  4. python打印倒等腰梯形,Linux使用shell脚本做的菱形等一些益智题
  5. mysql的分库分表
  6. android view绘制过程
  7. 关闭注册表编辑器,重新启动计算机后生效。这样资源管理器重启后就不会自动重启了。...
  8. phoenix Explain Plan 翻译
  9. 设置devenv命令的启动版本
  10. 力扣235. 二叉搜索树的最近公共祖先(JavaScript)
  11. fetchrow_array()与fetchrow_arrayref()与fetchrow_hashref()的使用方法
  12. uoj#267. 【清华集训2016】魔法小程序(乱搞)
  13. Spring中Bean的作用域差别
  14. 这就是神经网络 7:深度学习-目标检测-超详细图解Faster R-CNN
  15. 首届“十大最具价值”互联网创新创业项目遴选榜单丨Xtecher联合中投协权威发布...
  16. 动态炫酷的404页面源码
  17. 强烈推荐10套开源免费的高品质源码,源码免费下载
  18. dellr710服务器添加硬盘步骤,DELL R710服务器在线扩硬盘
  19. 嵌入式课后总结(3)
  20. ADI官网 hdl代码及sdk工程编译过程

热门文章

  1. XenDesktop5.0 Add Host使用vSphere5.1客户端注意事项
  2. EditText的各种属性
  3. 原理分析之:从JDBC到Mybatis
  4. 编程之美-2.6-精确表达浮点数
  5. 继SqlPager之后推出一款可用于前台页面的分页控件--UrlPager
  6. fabric-smaple部署
  7. Nowcoder 提高组练习赛-R3
  8. 大二暑假周进度报告(三)
  9. Oracle DBlink相关
  10. 使用Spring2.5的Autowired实现注释型的IOC(转载)