package com.hj.jd.basic.utils.web;import org.apache.http.HttpStatus;import org.apache.http.NameValuePair;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.HttpClient;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.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.io.UnsupportedEncodingException;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;import java.util.Map;public class HttpClientUtils { public static String doGet(String url) { return doGet(url, null, null); } public static String doGet(String url, Map<String, String> params) { return doGet(url, params, null); } public static String doGet(String url, Map<String, String> params, Map<String, String> headers) { //1.建立http客戶端對象 CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = null; String result = ""; try { //2.構建一個URI對象,根據url及參數 URIBuilder uriBuilder = new URIBuilder(url); //2.1請求參數 if (params != null && params.size() > 0) { for (String key : params.keySet()) { uriBuilder.addParameter(key, params.get(key)); } } URI uri = uriBuilder.build(); //3.建立一個get請求 HttpGet httpGet = new HttpGet(uri); //3.1請求頭 if (headers != null && headers.size() > 0) { for (String key : headers.keySet()) { httpGet.addHeader(key, headers.get(key)); } } //4.執行get請求,獲得響應 response = httpClient.execute(httpGet); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { //5.從響應對象中獲取響應數據 result = EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (URISyntaxException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { org.apache.http.client.utils.HttpClientUtils.closeQuietly(response); org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpClient); } return result; } public static String doPost(String url) { return doPost(url, null); } public static String doPost(String url, Map<String, String> params) { String result = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); //封裝post請求參數 int size = params.size(); if (params != null && size > 0) { List<NameValuePair> pairList = new ArrayList<>(size); for (String key : params.keySet()) { pairList.add(new BasicNameValuePair(key, params.get(key))); } try { UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(pairList); httpPost.setEntity(formEntity); //執行post請求 CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } finally { org.apache.http.client.utils.HttpClientUtils.closeQuietly(response); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpClient); return result; } public static String doPostWithJson(String url, String jsonParams) { String result = ""; CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); StringEntity entity = new StringEntity(jsonParams, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); CloseableHttpResponse response = null; try { response = httpClient.execute(httpPost); result = EntityUtils.toString(response.getEntity(), "UTF-8"); } catch (IOException e) { e.printStackTrace(); } finally { org.apache.http.client.utils.HttpClientUtils.closeQuietly(response); } org.apache.http.client.utils.HttpClientUtils.closeQuietly(httpClient); return result; } public static void main(String[] args) { String result = doGet("https://www.baidu.com", null, null); System.err.println(result); }}