Java+HttpClient库 封装get、键值对格式的post、json格式的post请求的工具方法【杭州多测师_王sir】
package cn.duoceshi.springbootdemo.utils; import cn.duoceshi.springbootdemo.Enum.CodeEnum; import cn.duoceshi.springbootdemo.model.HttpClientResponse; import org.apache.http.Header; import org.apache.http.NameValuePair; 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.methods.HttpRequestBase; import org.apache.http.client.utils.URIBuilder; import org.apache.http.entity.ContentType; 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.HashMap; import java.util.List; import java.util.Map; /** * 2023-8-30号 多测师 * 封装get、键值对格式的post、json格式的post请求的工具方法 */ public class HttpClientUtils { public static HttpClientResponse doGet(String url, Map<String, String> param){ return doGet(url, param, null); } public static HttpClientResponse doGet(String url, Map<String, String> param, Map<String, String> headers) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); HttpClientResponse response = null; try { // 创建uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); packageHeader(httpGet, headers); response = getHttpClientResponse(httpclient, httpGet); } catch (Exception e) { e.printStackTrace(); } finally { try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return response; } private static HttpClientResponse getHttpClientResponse(CloseableHttpClient httpclient, HttpRequestBase httpBase) throws IOException { // 执行请求 CloseableHttpResponse response = httpclient.execute(httpBase); // 封装成自定义的响应对象 if (response == null){ return HttpClientResponse.builder().status(CodeEnum.EXCEPTION.getCode()).build(); //响应为空返回500 } int status = response.getStatusLine().getStatusCode(); Header[] headers = response.getAllHeaders(); Map<String, String> map = new HashMap<>(); for (Header header : headers){ map.put(header.getName(), header.getValue()); } String body = EntityUtils.toString(response.getEntity(), "UTF-8"); return HttpClientResponse.builder() .status(status) .headers(map) .body(body).build(); } public static HttpClientResponse doPost(String url, Map<String, String> param){ return doPost(url, param, null); } /** * 表单类型的post * * @param url * @param param * @return */ public static HttpClientResponse doPost(String url, Map<String, String> param, Map<String, String> headers) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); HttpClientResponse response = null; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建参数列表 if (param != null) { List<NameValuePair> paramList = new ArrayList(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } // 模拟表单 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } // 添加header packageHeader(httpPost, headers); // 执行http请求 response = getHttpClientResponse(httpClient, httpPost); } catch (Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return response; } public static HttpClientResponse doPostJson(String url, String json){ return doPostJson(url, json, null); } /** * json类型的post接口 * @param url * @param json * @return */ public static HttpClientResponse doPostJson(String url, String json, Map<String, String> headers) { // 创建Httpclient对象 CloseableHttpClient httpClient = HttpClients.createDefault(); HttpClientResponse response = null; try { // 创建Http Post请求 HttpPost httpPost = new HttpPost(url); // 创建请求内容 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); // 添加header packageHeader(httpPost, headers); // 执行http请求 response = getHttpClientResponse(httpClient, httpPost); } catch (Exception e) { e.printStackTrace(); } finally { try { httpClient.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return response; } private static void packageHeader(HttpRequestBase httpBase, Map<String, String> headers) { //如果headers不为空、拿到键值对并且设置到请求头当中去 if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpBase.setHeader(entry.getKey(), entry.getValue()); } } } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
2022-08-30 杭州市民卡面试题【杭州多测师】【杭州多测师_王sir】
2020-08-30 java编程语言中的多态【多测师_王sir】