Interface HttpClient Class CloseableHttpClient 一个是接口,一个是实现该接口的类
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import org.apache.commons.lang3.StringUtils; import org.apache.http.HttpEntity; 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.utils.URIBuilder; 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.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Map.Entry; /** * @Description Http远程调用 * @Author WangKun * @Date 2019/10/31 14:19 * @Version */ public class HttpClientUtil { private static final Logger looger = LoggerFactory.getLogger(HttpClientUtil.class); /** * @param url * @param params * @throws * @Description GET请求 * @Return java.lang.Object * @Date 2019-10-31 15:38:01 * @Author WangKun **/ public static Object sendGetByMap(String url, Map<String, String> params) throws Exception { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); URIBuilder uriBuilder = new URIBuilder(url); //设置参数 if (null != params && params.size() > 0) { for (Entry<String, String> entry : params.entrySet()) { uriBuilder.addParameter(entry.getKey(), entry.getValue()); } } HttpGet httpGet = new HttpGet(uriBuilder.build()); // httpGet.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")); httpGet.setHeader(new BasicHeader("Content-Type", "application/json;charset=UTF-8")); // 执行get response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); if (StringUtils.isNotBlank(result)) { return JSONArray.parseArray(result); } } catch (Exception e) { looger.error("HttpClientUtil GET请求失败!", e); } finally { //释放资源,必须关闭,否则线程池不回收 response.close(); client.close(); } return null; } /** * @param url * @param params * @throws * @Description POST请求 * @Return java.lang.Object * @Date 2019-10-31 15:41:57 * @Author WangKun **/ public static Object sendPostByMap(String url, Map<String, String> params) throws Exception { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); List<NameValuePair> nameValuePairList = new ArrayList<>(); // 设置参数 if (null != params && params.size() > 0) { for (Entry<String, String> entry : params.entrySet()) { nameValuePairList.add(new BasicNameValuePair(entry.getKey(), entry.getValue())); } StringEntity entity = new UrlEncodedFormEntity(nameValuePairList, "UTF-8"); post.setEntity(entity); } post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8")); post.setHeader(new BasicHeader("Accept", "text/plain;charset=utf-8")); // 执行post response = client.execute(post); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); if (StringUtils.isNotBlank(result)) { return JSONObject.parseObject(result); } } catch (Exception e) { looger.error("HttpClientUtil POST请求失败!", e); } finally { //释放资源,必须关闭,否则线程池不回收 response.close(); client.close(); } return null; } /** * @param url * @param json * @throws * @Description POST请求 * @Return java.lang.Object * @Date 2019-10-31 16:26:14 * @Author WangKun **/ public static String sendPostByJson(String url, String json) throws Exception { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); HttpPost post = new HttpPost(url); StringEntity requestEntity = new StringEntity(json, "UTF-8"); requestEntity.setContentEncoding("UTF-8"); post.setHeader("Content-Type", "application/json;charset=UTF-8"); post.setEntity(requestEntity); // 执行post response = client.execute(post); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); if (StringUtils.isNotBlank(result)) { return result; } } catch (Exception e) { looger.error("HttpClientUtil POST请求失败!", e); } finally { //释放资源,必须关闭,否则线程池不回收 response.close(); client.close(); } return null; } /** * @param url * @throws * @Description 发送POST 请求没有参数 * @Return java.lang.Object * @Date 2019-11-06 10:59:34 * @Author WangKun **/ public static String sendGet(String url) throws Exception { CloseableHttpClient client = null; CloseableHttpResponse response = null; try { client = HttpClients.createDefault(); URIBuilder uriBuilder = new URIBuilder(url); HttpGet httpGet = new HttpGet(uriBuilder.build()); httpGet.setHeader(new BasicHeader("Content-Type", "application/json;charset=UTF-8")); // 执行get response = client.execute(httpGet); HttpEntity entity = response.getEntity(); String result = EntityUtils.toString(entity, "UTF-8"); if (StringUtils.isNotBlank(result)) { return result; } } catch (Exception e) { looger.error("HttpClientUtil GET请求失败!", e); } finally { //释放资源,必须关闭,否则线程池不回收 response.close(); client.close(); } return null; } }