HttpClientUtil请求http地址的工具类
直接贴代码:
import java.io.IOException;
import java.util.HashMap;
import java.util.Map; import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
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.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; /**
* HttpClientUtil
*/
public class HttpClientUtil { // 连接主机超时(30s)
public static final int HTTP_CONNECT_TIMEOUT_30S = 30 * 1000; // 从主机读取数据超时(3min)
public static final int HTTP_READ_TIMEOUT_3MIN = 180 * 1000; /**
* httpPost
*/
public static String httpPost(String url, String jsonParam) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url); // 设置请求头和请求参数
if (null != jsonParam && !jsonParam.isEmpty()) {
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
} // 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpPost.setConfig(requestConfig); // 发送请求
CloseableHttpResponse response = httpclient.execute(httpPost); // 获取返回内容
try {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
EntityUtils.consume(entity); // 此句关闭了流
return str;
} finally {
response.close();
}
} /**
* httpPost get Cookies
*/
public static Map<String, Object> httpPostGetCookies(String url, String jsonParam) throws ClientProtocolException,
IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url); // 设置请求头和请求参数
if (null != jsonParam && !jsonParam.isEmpty()) {
StringEntity entity = new StringEntity(jsonParam, "utf-8");
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
} // 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpPost.setConfig(requestConfig); // 发送请求
CloseableHttpResponse response = httpclient.execute(httpPost); // 获取返回内容
try {
HttpEntity entity = response.getEntity();
String str = EntityUtils.toString(entity);
EntityUtils.consume(entity); // 此句关闭了流 // 获取数据内容
Map<String, Object> map = new HashMap<String, Object>();
map.put("result", str); // 获取返回到额Cookies
Header[] headers = response.getHeaders("Set-Cookie");
map.put("cookies", headers); return map;
} finally {
response.close();
}
} /**
* httpGet
*/
public static String httpGet(String url) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url); // 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpGet.setConfig(requestConfig); // 发送请求
CloseableHttpResponse response = httpclient.execute(httpGet); // 获取返回内容
try {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity);
return strResult;
} finally {
response.close();
}
} /**
* httpGet with Cookies
*/
public static String httpGetWithCookies(String url, Header[] headers) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url); // 超时时间设置
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(HTTP_READ_TIMEOUT_3MIN)
.setConnectTimeout(HTTP_CONNECT_TIMEOUT_30S).build();
httpGet.setConfig(requestConfig); // 设置请求头
if (headers != null && headers.length > 0) {
httpGet.setHeaders(headers);
} // 发送请求
CloseableHttpResponse response = httpclient.execute(httpGet); // 获取返回内容
try {
HttpEntity entity = response.getEntity();
String strResult = EntityUtils.toString(entity);
return strResult;
} finally {
response.close();
}
} }
HttpClientUtil请求http地址的工具类的更多相关文章
- Http请求get、post工具类
在网上找了好久都没有找到post.get请求的工具类,现在整理了一下分享出来.http工具类如下: package com.qlwb.business.util; import java.io.Buf ...
- Java模拟http请求调用远程接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- Java模拟http请求远程调用接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- c# 外挂操作(内存操作)(内存读写取窗口句柄移动窗口取模块地址)工具类
来源于网上 参考 https://www.cnblogs.com/fuhua/p/5877781.html 等众多文章 详情取看我第二个例子封装功能较多 https://www.cnblogs.co ...
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...
- 获取请求头中User-Agent工具类
public class AgentUserKit { private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)&q ...
- Servlet里面request处理外部POST请求的输入流的工具类
package etcom.servlet; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- 【工具类】获取请求头中User-Agent工具类
public class AgentUserKit { private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)&q ...
- HttpClientUntils工具类的使用测试及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)
HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; ...
随机推荐
- Urllib3 库详解
文档:http://urllib3.readthedocs.io/en/latest/
- office2016选择性安装
office2016在安装的时候并不像之前的版本有选择性的安装,安装器会安装一个office全家桶.那么如何自主选择安装自己需要的工具呢? 微软在下载中心中提供了Office2016部署工具(Offi ...
- nvalid bound statement (not found)
问题详情: org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.coocaa.te ...
- USB集线器基础知识
1.USB集线器又称为USB Hub,用于拓展计算机USB接口.计算机主板上对外往往提供多个USB接口,这些接口往往都是通过主板上的USB集线器芯片来拓展出来的.我们平常用的可以连接多个USB接口的设 ...
- pandas Series KeyError: -1
前几天写分析方法,遇到的一个错误: 具体我已经在stackoverflow 里面得到了详细的解答,下面我把问题和解决办法总结一下,方便日后的回顾 问题: sql = "select {}, ...
- Ext3.4实现增删查改(form版)
var TaskPolicyStore = new Ext.data.JsonStore( { autoLoad : false, url : 'PolicyServlet?method= ...
- Wifi 评分机制分析
从android N开始,引入了wifi评分机制,选择wifi的时候会通过评分来选择. android O源码 frameworks\opt\net\wifi\service\java\com\and ...
- windows下docker使用及注意事项
1.windows container模式下,pull镜像会失败 no matching manifest for windows/amd64 in the manifest list entries ...
- Linq“条件排序”
StockQuantities.OrderBy(u=>u.Status==null) 该排序先排结果为0(false)的,再排结果为1(true)的 使用场景: 一个对象有上传时间(可以为空)和 ...
- SpringMVC自定义视图Excel视图和PDF视图
SpringMVC自定义视图 Excel视图和PDF视图 SpringMVC杂记(十一) 使用Excel视图 Spring MVC 视图解析器(ViewResolver ) java实现导出excel ...