直接贴代码:

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地址的工具类的更多相关文章

  1. Http请求get、post工具类

    在网上找了好久都没有找到post.get请求的工具类,现在整理了一下分享出来.http工具类如下: package com.qlwb.business.util; import java.io.Buf ...

  2. Java模拟http请求调用远程接口工具类

    package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  3. Java模拟http请求远程调用接口工具类

    package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  4. c# 外挂操作(内存操作)(内存读写取窗口句柄移动窗口取模块地址)工具类

    来源于网上  参考 https://www.cnblogs.com/fuhua/p/5877781.html 等众多文章 详情取看我第二个例子封装功能较多 https://www.cnblogs.co ...

  5. 网络请求以及网络请求下载图片的工具类 android开发java工具类

    package cc.jiusan.www.utils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; ...

  6. 获取请求头中User-Agent工具类

    public class AgentUserKit { private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)&q ...

  7. Servlet里面request处理外部POST请求的输入流的工具类

    package etcom.servlet; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...

  8. 【工具类】获取请求头中User-Agent工具类

    public class AgentUserKit { private static String pattern = "^Mozilla/\\d\\.\\d\\s+\\(+.+?\\)&q ...

  9. HttpClientUntils工具类的使用测试及注意事项(包括我改进的工具类和Controller端的注意事项【附 Json 工具类】)

    HttpClient工具类(我改过): package com.taotao.httpclient; import java.io.IOException; import java.net.URI; ...

随机推荐

  1. librtmp编译for android and ios 不要openssl

    git clone git://git.ffmpeg.org/rtmpdump 不想要openssl 在rtmp.h里面 #undef CRYPTO 编译动态库与静态库只需要修改下面的 #includ ...

  2. python程序中的调试:pdb

    python中,也有像c/c++语言的 gdb 一样的调试程序,即pdb: 只简单说明一个怎么使用它. 假设已经有了一个python程序名为 test.py, 我们在命令行输入以下内容,即可以进行 p ...

  3. Spring JDBC调用存储函数

    以下示例将演示spring jdbc如何调用存储函数.在这个示例中将通过调用存储函数来读取Student表中的一个可用记录信息.传递一个ID并获取学生的姓名. 语法: SimpleJdbcCall j ...

  4. ibatis中的安全问题

    http://blog.csdn.net/yangqillohe/article/details/4139265

  5. tracert

    TRACERT命令 编辑 Tracert(跟踪路由)是路由跟踪实用程序,用于确定 IP数据包访问目标所采取的路径.Tracert 命令使用用 IP 生存时间 (TTL) 字段和 ICMP 错误消息来确 ...

  6. Bind-DLZ with MySQL

    系统环境: 系统:centos 6.8 Mysql: 5.1 BIND: bind--P2.tar.gz IP地址:192.168.153.130 软件下载地址:http://ftp.isc.org/ ...

  7. 前端不容错过的jQuery图片滑块插件

    作为前端开发者,我们会碰到很到各种各样的jQuery插件,但老实说,很少有自己写的.今天要分享的几款jQuery图片滑块插件,也就是jQuery焦点图插件,基本上会在每个网站都有应用,可以下载看看,也 ...

  8. 按键精灵如何批量复制文本,再往excel里面一次性粘贴?

    原帖地址 http://zhidao.baidu.com/link?url=M2A9E1JF7wAzjtxMQG9uiW_PvP39HVlfwn6zDMzk9m6U05JA37SrgDcrVXg_c9 ...

  9. Git使用教程,从入门到上瘾

    Git 是什么? Git 是一个分布式的代码管理容器,本地和远端都保有一份相同的代码. Git 仓库主要是由是三部分组成:本地代码,缓存区,提交历史,这几乎是所有操作的本质,但是为了文章更加简单易懂, ...

  10. Python中使用MongoEngine

    pymongo来操作MongoDB数据库,但是直接把对于数据库的操作代码都写在脚本中,这会让应用的代码耦合性太强,而且不利于代码的优化管理 一般应用都是使用MVC框架来设计的,为了更好地维持MVC结构 ...