HttpClient Java工具类

news/2025/4/1 20:02:28/

HttpClient工具类

package com.sky.utils;import com.alibaba.fastjson.JSONObject;
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.client.utils.URIBuilder;
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;import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;/*** Http工具类*/
public class HttpClientUtil {static final  int TIMEOUT_MSEC = 5 * 1000;/*** 发送GET方式请求* @param url* @param paramMap* @return*/public static String doGet(String url,Map<String,String> paramMap){// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();String result = "";CloseableHttpResponse response = null;try{URIBuilder builder = new URIBuilder(url);if(paramMap != null){for (String key : paramMap.keySet()) {builder.addParameter(key,paramMap.get(key));}}URI uri = builder.build();//创建GET请求HttpGet httpGet = new HttpGet(uri);//发送请求response = httpClient.execute(httpGet);//判断响应状态if(response.getStatusLine().getStatusCode() == 200){result = EntityUtils.toString(response.getEntity(),"UTF-8");}}catch (Exception e){e.printStackTrace();}finally {try {response.close();httpClient.close();} catch (IOException e) {e.printStackTrace();}}return result;}/*** 发送POST方式请求* @param url* @param paramMap* @return* @throws IOException*/public static String doPost(String url, Map<String, String> paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);// 创建参数列表if (paramMap != null) {List<NameValuePair> paramList = new ArrayList();for (Map.Entry<String, String> param : paramMap.entrySet()) {paramList.add(new BasicNameValuePair(param.getKey(), param.getValue()));}// 模拟表单UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList);httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "UTF-8");} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}/*** 发送POST方式请求* @param url* @param paramMap* @return* @throws IOException*/public static String doPost4Json(String url, Map<String, String> paramMap) throws IOException {// 创建Httpclient对象CloseableHttpClient httpClient = HttpClients.createDefault();CloseableHttpResponse response = null;String resultString = "";try {// 创建Http Post请求HttpPost httpPost = new HttpPost(url);if (paramMap != null) {//构造json格式数据JSONObject jsonObject = new JSONObject();for (Map.Entry<String, String> param : paramMap.entrySet()) {jsonObject.put(param.getKey(),param.getValue());}StringEntity entity = new StringEntity(jsonObject.toString(),"utf-8");//设置请求编码entity.setContentEncoding("utf-8");//设置数据类型entity.setContentType("application/json");httpPost.setEntity(entity);}httpPost.setConfig(builderRequestConfig());// 执行http请求response = httpClient.execute(httpPost);resultString = EntityUtils.toString(response.getEntity(), "UTF-8");} catch (Exception e) {throw e;} finally {try {response.close();} catch (IOException e) {e.printStackTrace();}}return resultString;}private static RequestConfig builderRequestConfig() {return RequestConfig.custom().setConnectTimeout(TIMEOUT_MSEC).setConnectionRequestTimeout(TIMEOUT_MSEC).setSocketTimeout(TIMEOUT_MSEC).build();}}

http://www.ppmy.cn/news/942079.html

相关文章

服务机器人应用

随着时代的发展&#xff0c;机器人技术在各个领域越来越普及。在服务领域&#xff0c;服务机器人的应用也越来越受到人们的欢迎。服务机器人将会在商业、医疗、教育、酒店等领域得到应用&#xff0c;并成为未来发展的趋势。 在商业领域中&#xff0c;服务机器人可以承担很多工作…

GPS测量中所涉及的时间系统

1. 简介 时间和空间是物质存在的基本形式。时间是基本物理量之一, 它反映了物质运动的顺序性和连续性。人们在生产、科学研究和日常生活中都离不开时间。 2. 有关时间系统的一些基本概念 时间是一个重要的物理量, 在 GPS 测量中对时间提出了很高的要求。如利用 GPS 卫星发射…

乔治华盛顿大学计算机科学专业,乔治华盛顿大学计算机专业简介

乔治华盛顿大学计算机专业简介 【客户情况】 客户姓名&#xff1a;Althus 进入服务时间&#xff1a;2011.4 毕业院校及专业&#xff1a;国内某普通一本大学 计算机专业 申请目标&#xff1a;美国综合排名150左右的学校 硬件条件&#xff1a;IBT 80 GPA: 2.8 GRE&#xff1a;110…

【论文解读】2017 STGCN: Spatio-Temporal Graph Convolutional Networks

一、简介 使用历史速度数据预测未来时间的速度。同时用于序列学习的RNN(GRU、LSTM等)网络需要迭代训练&#xff0c;它引入了逐步累积的误差&#xff0c;并且RNN模型较难训练。为了解决以上问题&#xff0c;我们提出了新颖的深度学习框架STGCN&#xff0c;用于交通预测。 二、…

bclinux执行df命令无反应解决方法、bclinux执行df命令卡死解决方法、进程追踪strace命令说明

文章目录 说明解决方法首先排除不是系统问题验证挂载信息追踪df进程strace命令安装strace命令定位df命令卡在哪解决卡住的进程问题解决&#xff0c;验证 说明 额 今天收到反馈&#xff0c;虚拟机执行df命令卡住 卡住状态如下图 解决方法 首先排除不是系统问题 很简单&…

关于 XPC

关于 XPC XPC 是 OS X 下的一种 IPC (进程间通信) 技术, 它实现了权限隔离, 使得 App Sandbox 更加完备. 首先&#xff0c;XPC 更多关注的是实现功能某种的方式&#xff0c;通常采用其他方式同样能够实现。并没有强调如果不使用 XPC&#xff0c;无法实现某些功能。 XPC 目的是提…

如何查看XPS文件

xps文件相当于离线版网页&#xff0c;WPS不支持查看XPS文件&#xff0c;Office也不支持查看XPS文件 今天就分享两种查看XPS文件的方法 第一种方法就是使用Speedpdf在线文件转换网站将XPS文件免费转换成PDF文件或者Word文件&#xff0c;这个样子就可以查看XPS文件。 第二种方…

PaxosLease

世上有很多选举算法,但最完美的莫过于Paxos,但把paxos用于master选举与用于value的选举有用众多不同之处,主要一点是二者执行频率不同。value选举需要频繁、高密度地执行paxos算法,每instance选举一个value。因此,原生的paxos算法无法满足高性能的要求;另一个问题是,pax…