package com.saye.hospitalgd.util; import com.alibaba.fastjson.JSON; import org.apache.http.HttpEntity; import org.apache.http.HttpStatus; 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.BasicHeader; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.HashMap; /** * @author thuang * @version 1.0 * @description: HTTP客户端工具类,兼容JDK8 * @date 2024/12/19 16:00 */ public class HttpClientUtil { private static final Logger log = LoggerFactory.getLogger(HttpClientUtil.class); /** * 发送GET请求 * @param url 请求URL * @param headers 请求头(可为null) * @return 响应结果 */ public static String doGet(String url, HashMap headers) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; String result = null; try { httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet(url); // 设置请求头 if (headers != null) { for (String key : headers.keySet()) { httpGet.setHeader(key, headers.get(key)); } } response = httpClient.execute(httpGet); if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } } else { log.error("GET请求失败,状态码: " + (response != null ? response.getStatusLine().getStatusCode() : "null")); } } catch (Exception e) { log.error("GET请求异常", e); } finally { try { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { log.error("关闭HTTP连接异常", e); } } return result; } /** * 发送POST请求(JSON格式) * @param url 请求URL * @param data 请求数据(JSON对象) * @return 响应结果 */ public static String doPost(String url, Object data) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; String result = null; try { httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("content-type", "application/json;charset=utf-8"); // 将数据转换为JSON字符串 String jsonData = JSON.toJSONString(data); StringEntity stringEntity = new StringEntity(jsonData, "UTF-8"); stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } } else { log.error("POST请求失败,状态码: " + (response != null ? response.getStatusLine().getStatusCode() : "null")); } } catch (Exception e) { log.error("POST请求异常", e); } finally { try { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { log.error("关闭HTTP连接异常", e); } } return result; } /** * 发送POST请求(表单格式) * @param url 请求URL * @param params 请求参数 * @return 响应结果 */ public static String doPostForm(String url, HashMap params) { CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; String result = null; try { httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.setHeader("content-type", "application/x-www-form-urlencoded;charset=utf-8"); // 构建表单数据 StringBuilder formData = new StringBuilder(); if (params != null) { for (String key : params.keySet()) { if (formData.length() > 0) { formData.append("&"); } formData.append(key).append("=").append(params.get(key)); } } StringEntity stringEntity = new StringEntity(formData.toString(), "UTF-8"); httpPost.setEntity(stringEntity); response = httpClient.execute(httpPost); if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { HttpEntity entity = response.getEntity(); if (entity != null) { result = EntityUtils.toString(entity, "UTF-8"); } } else { log.error("POST表单请求失败,状态码: " + (response != null ? response.getStatusLine().getStatusCode() : "null")); } } catch (Exception e) { log.error("POST表单请求异常", e); } finally { try { if (response != null) { response.close(); } if (httpClient != null) { httpClient.close(); } } catch (IOException e) { log.error("关闭HTTP连接异常", e); } } return result; } }