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; /** * No. 2023-8-30 Multiple testers * Tool methods that encapsulate get, post in key-value format, and post requests in json format */ 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) { //Create Httpclient object CloseableHttpClient httpclient = HttpClients.createDefault(); HttpClientResponse response = null; try { //Create uri URIBuilder builder = new URIBuilder(url); if (param != null) { for (String key : param.keySet()) { builder.addParameter(key, param.get(key)); } } URI uri = builder.build(); // Create http GET request 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 { //Execute the request CloseableHttpResponse response = httpclient.execute(httpBase); // Encapsulate into a custom response object if (response == null){ return HttpClientResponse.builder().status(CodeEnum.EXCEPTION.getCode()).build(); //The response is empty and returns 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); } /** *Form type post * * @param url * @param param * @return */ public static HttpClientResponse doPost(String url, Map<String, String> param, Map<String, String> headers) { //Create Httpclient object CloseableHttpClient httpClient = HttpClients.createDefault(); HttpClientResponse response = null; try { // Create Http Post request HttpPost httpPost = new HttpPost(url); //Create parameter list if (param != null) { List<NameValuePair> paramList = new ArrayList(); for (String key : param.keySet()) { paramList.add(new BasicNameValuePair(key, param.get(key))); } // Simulate form UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); httpPost.setEntity(entity); } //Add header packageHeader(httpPost, headers); //Execute http request 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 type post interface * @param url * @param json * @return */ public static HttpClientResponse doPostJson(String url, String json, Map<String, String> headers) { //Create Httpclient object CloseableHttpClient httpClient = HttpClients.createDefault(); HttpClientResponse response = null; try { // Create Http Post request HttpPost httpPost = new HttpPost(url); //Create request content StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); //Add header packageHeader(httpPost, headers); //Execute http request 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) { //If headers is not empty, get the key-value pair and set it to the request header. if (headers != null) { for (Map.Entry<String, String> entry : headers.entrySet()) { httpBase.setHeader(entry.getKey(), entry.getValue()); } } } }