Loading... ``` package cn.dz.util.HttpRequestFilesUtils; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; 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.*; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * @return * @Author 茶白 * @date 2021/3/31 11:58 */ public class HttpClientUtil { // 发送请求编码 UTF-8 private static final String ENCODING = "UTF-8"; // 超时时间 毫秒 private static final int CONNECT_TIMEOUT = 10000; // 请求获取数据的超时时间(即响应时间),单位毫秒。 private static final int SOCKET_TIMEOUT = 5000; /** * Description: get请求 * * @param url 路径 * @return 响应结果 */ public static String get(String url) throws Exception { return get(url, null); } /** * Description: post请求 * * @param url 路径 * @param params 参数 * @return 响应结果 */ public static String post(String url, Map<String, Object> params) throws Exception { return post(url, params, null); } /** * Description: post请求(用于请求json格式的参数) * * @param url 路径 * @param params json参数 * @return 响应结果 */ public static String jsonPost(String url, String params) throws Exception { return jsonPost(url, params, null); } /** * Description: get请求 * * @param url 路径 * @param headers 协议头 * @return 响应结果 */ public static String get(String url, Map<String, String> headers) throws Exception { CloseableHttpClient httpClient = null; HttpGet httpGet; CloseableHttpResponse httpResponse = null; String result; try { httpClient = HttpClientBuilder.create().build(); httpGet = new HttpGet(url); //设置协议头 setHeader(headers, httpGet); RequestConfig config = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpGet.setConfig(config); result = getHttpClientResult(httpResponse, httpClient, httpGet); } finally { release(httpResponse, httpClient); } return result; } /** * Description: post请求 * * @param url 路径 * @param params 参数 * @param headers 协议头 * @return 响应结果 */ public static String post(String url, Map<String, Object> params, Map<String, String> headers) throws Exception { CloseableHttpClient httpClient = null; HttpPost httpPost; CloseableHttpResponse httpResponse = null; String result; try { httpClient = HttpClientBuilder.create().build(); httpPost = new HttpPost(url); //设置协议头 setHeader(headers, httpPost); //设置参数 setParam(params, httpPost); RequestConfig config = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpPost.setConfig(config); result = getHttpClientResult(httpResponse, httpClient, httpPost); } finally { release(httpResponse, httpClient); } return result; } /** * Description: post请求(用于请求json格式的参数) * * @param url 路径 * @param params JSON参数 * @param headers 协议头 * @return 响应结果 */ public static String jsonPost(String url, String params, Map<String, String> headers) throws Exception { CloseableHttpClient httpClient = null; HttpPost httpPost; CloseableHttpResponse httpResponse = null; String result; try { httpClient = HttpClientBuilder.create().build(); httpPost = new HttpPost(url); httpPost.addHeader("Content-type", "application/json; charset=utf-8"); httpPost.setHeader("Accept", "application/json"); //设置协议头 setHeader(headers, httpPost); //设置参数 httpPost.setEntity(new StringEntity(params, ENCODING)); RequestConfig config = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build(); httpPost.setConfig(config); result = getHttpClientResult(httpResponse, httpClient, httpPost); } finally { release(httpResponse, httpClient); } return result; } /** * Description: 封装协议头 * @param headers 协议头 * @param httpMethod httpMethod */ public static void setHeader(Map<String, String> headers, HttpRequestBase httpMethod) { // 封装协议头 if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpMethod.addHeader(entry.getKey(), entry.getValue()); } } } /** * Description: 封装请求参数数据 * @param params 请求参数 * @param httpMethod httpMethod */ public static void setParam(Map<String, Object> params, HttpEntityEnclosingRequestBase httpMethod) throws UnsupportedEncodingException { List<NameValuePair> list = new ArrayList<>(); if (null != params) { for (Entry<String, Object> entry : params.entrySet()) { String key = entry.getKey(); String value = entry.getValue() != null ? entry.getValue().toString() : ""; list.add(new BasicNameValuePair(key, value)); } } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, ENCODING); httpMethod.setEntity(entity); } } /** * Description: 获取请求结果 * @param httpResponse httpResponse * @param httpClient httpClient * @param httpMethod httpMethod * @return 请求结果 * @throws Exception Exception */ public static String getHttpClientResult(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception { String result = null; // 执行请求 httpResponse = httpClient.execute(httpMethod); // 获取返回结果 if (httpResponse != null) { HttpEntity resEntity = httpResponse.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, ENCODING); } } return result; } /** * Description: 释放资源 * @param httpResponse HttpResponse * @param httpClient HttpClient * @throws IOException IOException */ public static void release(CloseableHttpResponse httpResponse, CloseableHttpClient httpClient) throws IOException { if (httpResponse != null) { httpResponse.close(); } if (httpClient != null) { httpClient.close(); } } } ``` 本人封装的HttpClient发送get post请求工具类,仅适用于个人研究,进阶版还需要自行完善 最后修改:2021 年 04 月 11 日 © 允许规范转载 打赏 赞赏作者 微信 赞 0 如果觉得我的文章对你有用,请随意赞赏
此处评论已关闭