HttpClientUtil 工具类
发布日期:2021-06-30 14:54:12
浏览次数:2
分类:技术文章
本文共 5937 字,大约阅读时间需要 19 分钟。
HttpClientUtil 工具类
依赖
org.apache.httpcomponents httpclient 4.5.2
工具类
package com.jerry.utls;package com.httpclinet;import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.entity.UrlEncodedFormEntity;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.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;/** * http工具类 * @author jerryjin */public class HttpClientUtil { private RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(15000) .setConnectTimeout(15000) .setConnectionRequestTimeout(15000) .build(); private volatile static HttpClientUtil instance = null; private HttpClientUtil(){ } public static HttpClientUtil getInstance(){ if (instance == null) { synchronized (HttpClientUtil.class) { if (instance == null) { instance = new HttpClientUtil(); } } } return instance; } /** * 发送 post请求 * @param httpUrl 地址 */ public String sendHttpPost(String httpUrl) { HttpPost httpPost = new HttpPost(httpUrl); return sendHttpPost(httpPost); } /** * 发送 post请求 * @param httpUrl 地址 * @param params 参数(格式:key1=value1&key2=value2) */ public String sendHttpPost(String httpUrl, String params) { HttpPost httpPost = new HttpPost(httpUrl); try { //设置参数 StringEntity stringEntity = new StringEntity(params, "UTF-8"); stringEntity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(stringEntity); } catch (Exception e) { e.printStackTrace(); } return sendHttpPost(httpPost); } /** * 发送 post请求 * @param httpUrl 地址 * @param maps 参数 */ public String sendHttpPost(String httpUrl, Mapmaps) { HttpPost httpPost = new HttpPost(httpUrl); // 创建参数队列 List nameValuePairs = new ArrayList (); for (String key : maps.keySet()) { nameValuePairs.add(new BasicNameValuePair(key, maps.get(key))); } try { httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8")); } catch (Exception e) { e.printStackTrace(); } return sendHttpPost(httpPost); } /** * 发送Post请求 * @param httpPost * @return */ private String sendHttpPost(HttpPost httpPost) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpPost.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpPost); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; } /** * 发送 get请求 * @param httpUrl */ public String sendHttpGet(String httpUrl) { HttpGet httpGet = new HttpGet(httpUrl); return sendHttpGet(httpGet); } /** * 发送Get请求 * @param httpGet * @return */ public String sendHttpGet(HttpGet httpGet) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; HttpEntity entity = null; String responseContent = null; try { // 创建默认的httpClient实例. httpClient = HttpClients.createDefault(); httpGet.setConfig(requestConfig); // 执行请求 response = httpClient.execute(httpGet); entity = response.getEntity(); responseContent = EntityUtils.toString(entity, "UTF-8"); } catch (Exception e) { e.printStackTrace(); } finally { try { // 关闭连接,释放资源 if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } return responseContent; }}
客户端
import org.apache.http.client.methods.HttpGet;/** * @author jerryjin * @Classname HttpClinetDemo * @Date 2021-04-11 12:36 */public class HttpClinetDemo { public static void main(String[] args) throws CloneNotSupportedException { String url = "https://jerryjin.blog.csdn.net/article/details/115766780"; HttpGet get = new HttpGet(url); get.addHeader("User-Agent", " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36"); String s = HttpClientUtil.getInstance().sendHttpGet(get); System.out.println(s); }}
转载地址:https://jerryjin.blog.csdn.net/article/details/115766780 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年05月01日 04时47分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Python基础学习篇-7-各种数据类型总结(一)
2019-05-01
Linux CentOS 部署 jenkins
2019-05-01
Linux CentOS7 安装gradle
2019-05-01
Linux 配置双机SSH信任实现免密远程登录
2019-05-01
Linux 安装 sdkman 及使用方法
2019-05-01
分别在Windows 和 Linux 搭建java环境
2019-05-01
性能测试工具JMeter的安装及环境配置--Windows和Linux
2019-05-01
linux安装、卸载、自定义配置 jenkins
2019-05-01
jmeter 一些测试用的方法
2019-05-01
[1] JMeter初相识
2019-05-01
压测时产生大量日志,导致磁盘占满怎么办?
2019-05-01
如何快速找出Linux系统中的大文件?
2019-05-01
【JMeter】02 响应断言和beanshell断言
2019-05-01
[3] JMeter -详解jmeter测试计划
2019-05-01
[4] JMeter-详解jmeter线程组
2019-05-01
如何使用mysql存储过程批量造压测数据?
2019-05-01
使用java代码和jmeter脚本批量造数
2019-05-01