package com.sgcc.utils; import cn.hutool.json.JSONObject; import java.net.URI; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import lombok.extern.slf4j.Slf4j; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; 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.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * Http请求工具类 * * @author zhaoyuanyuan * @version 1.0.0 * @date 2024/06/24 17:14 */ @Slf4j public class HttpsUtils { /** * 字符编码utf-8 */ public static final String CHARSET_UTF8 = "UTF-8"; /** * https请求url前缀 */ public static final String HTTPS_PREFIX = "https"; /** * 认证信息 */ public static final String AUTHORIZATION = "Authorization"; /** * post提交 * * @param url url * @param map 请求入参 * @param header 头信息 * @return {@code String} */ public static String doPost(String url, Map map, Map header) { return doPost(url, map, CHARSET_UTF8, header); } /** * post请求 * * @param url url * @param map 请求入参 * @param charset 字符集 * @param header 头 * @return {@code String} */ public static String doPost(String url, Map map, String charset, Map header) { HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { httpClient = new DefaultHttpClient(); httpPost = new HttpPost(url); //设置参数 List list = new ArrayList(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry elem = (Map.Entry) iterator.next(); list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { Map param = new HashMap<>(); param.putAll(map); log.info("httpParam:{}", param); StringEntity stringEntity = new StringEntity(new JSONObject(param).toString()); httpPost.setEntity(stringEntity); } if (header.size() > 0) { //设置请求头 Iterator entryList = header.entrySet().iterator(); while (entryList.hasNext()) { Map.Entry entry = (Map.Entry) entryList.next(); httpPost.setHeader(entry.getKey(), entry.getValue()); } } HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception e) { log.error("HttpsUtils doPost exception: {}", e.getMessage()); } return result; } /** * https Get 请求方式 * * @param url 请求url * @param header 请求头 * @return {@code String } * @author zhaoyuanyuan * @date 2024/06/24 17:16 */ public static String doGet(String url, Map header) { return doGet(url, header, new HashMap<>()); } /** * https Get 请求方式 * * @param url url * @param header 请求头 * @param uriVariable 请求参数 * @return {@code String } * @author zhaoyuanyuan * @date 2024/06/24 17:16 */ public static String doGet(String url, Map header, Map uriVariable) { HttpClient httpClient = null; HttpGet httpGet = null; String result = null; try { httpClient = new DefaultHttpClient(); URIBuilder uriBuilder = new URIBuilder(url); // 请求参数 if (uriVariable != null) { uriVariable.forEach(uriBuilder::addParameter); } URI uri = uriBuilder.build(); httpGet = new HttpGet(uri); log.info("HttpsUtils doGet: {}", httpGet.getRequestLine().getUri()); // 请求头 if (header != null) { header.forEach(httpGet::setHeader); } HttpResponse response = httpClient.execute(httpGet); if (response != null) { HttpEntity resEntity = response.getEntity(); log.info("HttpsUtils doGet response status: {}", response.getStatusLine().getStatusCode()); if (resEntity != null) { result = EntityUtils.toString(resEntity, CHARSET_UTF8); } } } catch (Exception e) { log.error("HttpsUtils doGet exception: {}", e.getMessage()); } return result; } }