一个封装的使用Apache HttpClient进行Http请求(GET、POST、PUT等)的类。
一个封装的使用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等)的类。的更多相关文章
- 网络相关系列之中的一个:Android中使用HttpClient发送HTTP请求
一.HTTP协议初探: HTTP(Hypertext Transfer Protocol)中文 "超文本传输协议",是一种为分布式,合作式,多媒体信息系统服务,面向应用层的协议,是 ...
- HttpClientUtil [使用apache httpclient模拟http请求]
基于httpclient-4.5.2 模拟http请求 以get/post方式发送json请求,并获取服务器返回的json -------------------------------------- ...
- 170314、工具:apache httpClient多线程并发情况下安全实用及工具类分享
简单用法介绍:介绍来源网络 建立连接:在HttpClient中使用多线程的一个主要原因是可以一次执行多个方法.在执行期间,每一个方法都使用一个HttpConnection实例.由于在同一时间多个连接只 ...
- 新旧apache HttpClient 获取httpClient方法
在apache httpclient 4.3版本中对很多旧的类进行了deprecated标注,通常比较常用的就是下面两个类了. DefaultHttpClient -> CloseableHtt ...
- Apache HttpClient组件封装工具类
package com.mengyao.spider.utils; import java.util.ArrayList;import java.util.HashMap;import java.ut ...
- Apache HttpClient使用之阻塞陷阱
前言: 之前做个一个数据同步的定时程序. 其内部集成了某电商的SDK(简单的Apache Httpclient4.x封装)+Spring Quartz来实现. 原本以为简单轻松, 喝杯咖啡就高枕无忧的 ...
- Apache HttpClient 读取响应乱码问题总结
Apache HttpClient 读取响应乱码问题总结 setCharacterEncoding Content-Type HttpClient 起因 最近公司产品线研发人员调整,集中兵力做战 ...
- 如何在Apache HttpClient中设置TLS版本
1.简介 Apache HttpClient是一个底层.轻量级的客户端HTTP库,用于与HTTP服务器进行通信. 在本教程中,我们将学习如何在使用HttpClient时配置支持的传输层安全(TLS)版 ...
- 论httpclient上传带参数【commons-httpclient和apache httpclient区别】
需要做一个httpclient上传,然后啪啪啪网上找资料 1.首先以前系统中用到的了commons-httpclient上传,找了资料后一顿乱改,然后测试 PostMethod filePost = ...
随机推荐
- Svg.Js 简介(转)
什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SVG 使用 XML 格式定义图形 SVG 图像在放大或改变尺 ...
- ss简单使用
ss简单使用 ss即socket state. 1.常用语句 ss -l 显示所有处于监听的网络接口连接 ss -pl 显示所有处于监听的网络接口连接,及相应的进程名称.进号等 ss -t -a 显示 ...
- Chapter 6 -- Caches
CachesExplained Explanation for how to use Guava caches. explained Updated Jun 4, 2013 by lowas...@g ...
- 阿里NLP总监分享-NLP技术的应用与思考
https://yq.aliyun.com/articles/78031 NLP技术的应用及思考
- Could not load file or assembly 'System.Data.SQLite' or one of its dependencies. An attempt was made to load a program
今天同事在一个服务器(winserver 2008 x64)上新建了一个IIS(7) 网站,但是报了如下错误: Could not load file or assembly 'System.Data ...
- 利用样式——android2.3实现android4.0风格的edittext
先看效果: 思路:在源码里找到4.0风格的图片作为背景,xml文件定义点击时候边框变化 步骤: ①.在F:\sdk\sdk\platforms\android-14\data\res\drawable ...
- 转:EM算法总结
https://applenob.github.io/em.html EM算法总结 在概率模型中,最常用的模型参数估计方法应该就是最大似然法. EM算法本质上也是最大似然,它是针对模型中存在隐变量的情 ...
- ICTCLAS中的HMM人名识别
http://www.hankcs.com/nlp/segment/ictclas-the-hmm-name-recognition.html 本文主要从代码的角度分析标注过程中的细节,理论谁都能说, ...
- VIM的buffers
原文:http://ju.outofmemory.cn/entry/13522 重新在不同的 tab 中打开多个关闭的buffer 文件, https://stackoverflow.com/ques ...
- Eclipse版本控制Git不能Pull或者Push
如下图,addWcLesson.jsp做了修改,但是却显示蓝色√,而且在Eclipse的Git提交插件中也没有监测到修改的文件,导致无法Push and Commit 原因:之前有些文件执行了 ...