HTTP请求客户端工具类
1.maven 引入依赖
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency> <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
2.HTTP请求客户端工具类代码
package com.vx.springbootexcel.common.utils; import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.internal.util.StringHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream; /**
* HTTP请求客户端工具类
*
* @author yaojz
* @version 1.0
*/
public class HttpClientUtil { // protected static InnerLogger logger = new GpLoggerWrapper().new InnerLogger(HttpClientUtil.class);
private static Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
private static HttpClient httpClient;
private static int HTTP_TIMEOUT = 60000; private HttpClientUtil() {
} private final static HttpClient getInstance() {
if (httpClient == null) {
MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
HttpConnectionManagerParams params = new HttpConnectionManagerParams();
params.setConnectionTimeout(HTTP_TIMEOUT);
params.setSoTimeout(HTTP_TIMEOUT);
params.setMaxTotalConnections(10000);
params.setDefaultMaxConnectionsPerHost(1000);
connectionManager.setParams(params);
httpClient = new HttpClient(connectionManager);
httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
}
return httpClient;
} /**
* 通过HTTP协议请求
*
* @param url 不能为空
* @param parameters 可为空
* @return 目标地址响应内容
* @throws Exception 网络异常
*/
public static String getRequest_ResponseBodyAsString(String url, Map<String, String> parameters) throws Exception {
if (url == null) {
logger.warn(" the url is null");
return "";
}
String params = parameterToString(parameters, "utf-8");
logger.debug("HttpGet请求:" + url + (params != null ? url.contains("?") ? "&" + params : "?" + params : ""));
GetMethod method = new GetMethod(url + (params != null ? url.contains("?") ? "&" + params : "?" + params : ""));
int statusCode;
try {
// method.addRequestHeader("Accept-Encoding", "gzip, deflate");
statusCode = HttpClientUtil.getInstance().executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: " + method.getStatusLine());
throw new Exception("请求的服务器有异常:服务器状态码为" + statusCode);
}
if (method.getResponseHeader("Content-Encoding") != null) {
String acceptEncoding = method.getResponseHeader("Content-Encoding").getValue();
if (acceptEncoding.toLowerCase().indexOf("gzip") > -1) {
// 建立gzip解压工作流
StringBuffer sb = new StringBuffer();
InputStream is = method.getResponseBodyAsStream();
GZIPInputStream gzin = new GZIPInputStream(is);
InputStreamReader isr = new InputStreamReader(gzin); // 设置读取流的编码格式
// ,
// 自定义编码
BufferedReader br = new BufferedReader(isr);
String tempbf;
while ((tempbf = br.readLine()) != null) {
sb.append(tempbf);
sb.append("\r\n");
}
isr.close();
gzin.close();
return sb.toString();
} }
return method.getResponseBodyAsString();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
method.releaseConnection();
}
} public static String streamRequest_ResponseBodyAsString(String url, String param) throws Exception {
if (StringHelper.isNullOrEmptyString(url)) {
logger.warn(" the url is null");
return "";
}
logger.debug("Http写流请求:" + url + (param != null ? url.contains("?") ? "&" + param : "?" + param : ""));
OutputStream out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Length", "" + Integer.toString(param.getBytes().length));
conn.setRequestProperty("Content-Type", "text/xml;charset=utf8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setReadTimeout(60000);
conn.setConnectTimeout(60000);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = conn.getOutputStream();
// 发送请求参数
out.write(param.getBytes("UTF-8"));
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
conn.disconnect();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
return result;
} public static String postRequest_ResponseBodyAsString(String url, Map<String, String> parameters) throws Exception {
if (StringHelper.isNullOrEmptyString(url)) {
logger.warn(" the url is null");
return "";
}
logger.debug("HttpPost请求:" + url + (parameters != null ? url.contains("?") ? "&" + parameters : "?" + parameters : "")); PostMethod method = new PostMethod(url);
int statusCode;
try {
for (String key : parameters.keySet()) {
method.addParameter(key, parameters.get(key));
}
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
statusCode = HttpClientUtil.getInstance().executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: " + method.getStatusLine());
throw new Exception("Http服务链路异常:服务器状态码为" + statusCode);
}
return method.getResponseBodyAsString();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
method.releaseConnection();
}
} /**
* 以POST方式发送请求
*
* @param url
* @param parameters
* @return 返回字节内容
* @throws Exception
*/
public static byte[] postRequest_ResponseBodyAsBytes(String url, Map<String, String> parameters) throws Exception {
logger.debug("HttpPost请求:" + url + (parameters != null ? url.contains("?") ? "&" + parameters : "?" + parameters : ""));
PostMethod method = new PostMethod(url);
int statusCode;
try {
for (String key : parameters.keySet()) {
method.addParameter(key, parameters.get(key));
}
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// method.setRequestHeader("Accept-Encoding", "gzip, deflate");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: " + method.getStatusLine());
throw new Exception("Http服务链路异常:服务器状态码为" + statusCode);
}
return method.getResponseBody();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
// Release the connection.
method.releaseConnection();
}
} /**
* 以POST方式发送请求
*
* @param url
* @param parameters
* @return 返回流
* @throws Exception
*/
public static InputStream postRequest_ResponseBodyAsStream(String url, Map<String, String> parameters) throws Exception {
logger.debug("HttpPost请求:" + url + (parameters != null ? url.contains("?") ? "&" + parameters : "?" + parameters : ""));
PostMethod method = new PostMethod(url);
int statusCode;
try {
for (String key : parameters.keySet()) {
method.addParameter(key, parameters.get(key));
}
method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
// method.setRequestHeader("Accept-Encoding", "gzip, deflate");
statusCode = httpClient.executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: " + method.getStatusLine());
throw new Exception("Http服务链路异常:服务器状态码为" + statusCode);
}
return method.getResponseBodyAsStream();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
// Release the connection.
method.releaseConnection();
}
} // 对参数编码组合成一个get请求参数串
private static String parameterToString(Map<String, String> parameters, String input_charset) {
if (parameters != null) {
StringBuffer result = new StringBuffer();
List<String> keys = new ArrayList<String>(parameters.keySet());
for (int i = 0; i < keys.size(); i++) {
try {
String key = (String) keys.get(i);
String value = (String) parameters.get(key);
if (i == keys.size() - 1) {
result.append(key).append("=").append(URLEncoder.encode(value, input_charset));
} else {
result.append(key).append("=").append(URLEncoder.encode(value, input_charset)).append("&");
}
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
}
}
return result.toString();
}
return null;
} /**
* 通过HTTP协议请求
*
* @param url 不能为空
* @param postBody 不能为空
* @param tokenId
* @return 目标地址响应内容
* @throws Exception 网络异常
*/
public static String postJSONRequest(String url, String postBody,String tokenId) throws Exception {
if (StringHelper.isNullOrEmptyString(url)) {
logger.warn(" the url is null");
return "";
}
logger.debug("HttpPost请求:" + url + postBody);
PostMethod method = new PostMethod();
try {
method.setURI(new URI(url, true, "UTF-8"));
} catch (URIException e1) {
throw new Exception("解析URL出错:" + url);
} catch (NullPointerException e1) {
throw new Exception("解析URL出错:" + url);
}
method.setRequestHeader("Content-type", "application/json; charset=UTF-8");
if(StringUtils.isNotBlank(tokenId)){
method.setRequestHeader("tokenId", tokenId);
}
RequestEntity requestEntity = new StringRequestEntity(postBody);
method.setRequestEntity(requestEntity);
int statusCode;
try {
statusCode = HttpClientUtil.getInstance().executeMethod(method);
if (statusCode != HttpStatus.SC_OK) {
logger.error("Method failed: " + method.getStatusLine());
throw new Exception("请求的服务器有异常:服务器状态码为" + statusCode);
}
BufferedReader in = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), method.getResponseCharSet()));
StringBuffer sb = new StringBuffer();
int chari2;
while ((chari2 = in.read()) != -1) {
sb.append((char) chari2);
}
return sb.toString();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
if (method != null) {
method.releaseConnection();
}
}
} public static String streamRequest_ResponseBodyAsStringWithUTF8(String url, String param) throws Exception {
if (StringHelper.isNullOrEmptyString(url)) {
logger.warn(" the url is null");
return "";
}
logger.debug("Http写流请求:" + url + (param != null ? url.contains("?") ? "&" + param : "?" + param : ""));
OutputStream out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("Content-Length", "" + Integer.toString(param.getBytes().length));
conn.setRequestProperty("Content-Type", "text/xml;charset=utf8");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestMethod("POST");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setReadTimeout(60000);
conn.setConnectTimeout(60000);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = conn.getOutputStream();
// 发送请求参数
out.write(param.getBytes("UTF-8"));
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
String line;
while ((line = in.readLine()) != null) {
result += "\n" + line;
}
conn.disconnect();
} catch (Throwable e) {
throw new Exception("Http服务链路异常:" + e.getMessage(), e);
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
}
return result.trim();
} }
HTTP请求客户端工具类的更多相关文章
- 带有连接池的Http客户端工具类HttpClientUtil
一.背景 业务开发中,经常会遇到通过http/https向下游服务发送请求.每次都要重复造轮子写HttpClient的逻辑,而且性能.功能参差不齐.这里分享一个高性能的.带连接池的通用Http客户端工 ...
- Redis进阶实践之九 独立封装的RedisClient客户端工具类
一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己写了两个工具类,但是这两个工具类,没有提供一致的接口,是为了使用的独立 ...
- Redis进阶实践之九 独立封装的RedisClient客户端工具类(转载9)
Redis进阶实践之九 独立封装的RedisClient客户端工具类 一.引言 今天开始有关Redis学习的第九篇文章了,以后肯定会大量系统使用Redis作为缓存介质,为了更好的更好的Redis,自己 ...
- HttpUtils 用于进行网络请求的工具类
原文:http://www.open-open.com/code/view/1437537162631 import java.io.BufferedReader; import java.io.By ...
- 发送http请求和https请求的工具类
package com.haiyisoft.cAssistant.utils; import java.io.IOException;import java.util.ArrayList; impor ...
- java中模拟http(https)请求的工具类
在java中,特别是java web中,我们经常需要碰到的一个场景是我们需要从服务端去发送http请求,获取到数据,而不是直接从浏览器输入请求网址获得相应.比如我们想访问微信接口,获取其返回信息. 在 ...
- 高德地图web端笔记;发送http请求的工具类
1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...
- 分享自己配置的HttpURLConnection请求数据工具类
>>该工具类传入string类型url返回string类型获取结果import java.io.BufferedReader;import java.io.InputStream;impo ...
- httputil用http获取请求的工具类
package com.xiaocan.demo.util; import java.io.IOException; import java.io.InputStream; import java.u ...
随机推荐
- Spring+Dubbo+TestNG接口测试初探
最近因工作原因,需要测试dubbo接口,通过公司同事写的框架,再结合度娘的帮助,自己做了一些总结记录. 通过下文意在说明如何搭建一个spring + dubbo + testng的测试环境,并完成一个 ...
- 最基础的分类算法-k近邻算法 kNN简介及Jupyter基础实现及Python实现
k-Nearest Neighbors简介 对于该图来说,x轴对应的是肿瘤的大小,y轴对应的是时间,蓝色样本表示恶性肿瘤,红色样本表示良性肿瘤,我们先假设k=3,这个k先不考虑怎么得到,先假设这个k是 ...
- 《构建之法》第五次作业——Alpha项目测试
博客开头 这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/2019autumnsystemanalysisanddesign?page=6 这个作业要求在 ...
- 个性化召回算法实践(四)——ContentBased算法
ContentBased算法的思想非常简单:根据用户过去喜欢的物品(本文统称为 item),为用户推荐和他过去喜欢的物品相似的物品.而关键就在于这里的物品相似性的度量,这才是算法运用过程中的核心. C ...
- POJ 2155 Matrix[树状数组+差分]
原题链接:https://vjudge.net/problem/POJ-2155 题目大意 给定 n* n 矩阵A,其元素为0或1. A [i][j] 表示第i行和第j列中的数字.最初全为0. 我们有 ...
- 在idea中如何将一个项目打成war包
s会用clean+package两个组合命令.来进行打包,我的war直接打在了target下面.然后,随时可以找到.war文件
- dos中查找端口的PID,并在任务管理器中处理端口
本文来源https://www.cnblogs.com/lsyf/p/8979012.html 1.查看所有端口进程 首先点击开始菜单选择运行,接着在运行对话框中输入“cmd”,回车打开命令提示符窗口 ...
- webpack4 打包优化
1 参考文章 彻底解决 webpack 打包文件体积过大 webpack4提升180%编译速度 详解webpack4之splitchunksPlugin代码包分拆 webpack v4 中的断舍离 开 ...
- java疑问
1. new String("abc")究竟创建几个对象? 答: 一个或两个, 如果常量池中原来有"abc", 那么只创建一个对象; 如果常量池中原来没有&qu ...
- Mac下mysql出现错误:ERROR 1055 (42000)
问题原因: ONLY_FULL_GROUP_BY的意思是:对于GROUP BY聚合操作,如果在SELECT中的列,没有在GROUP BY中出现,那么这个SQL是不合法的,因为列不在GROUP BY从句 ...