package com.jt.util;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.config.RequestConfig;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.impl.client.CloseableHttpClient;import org.apache.http.message.BasicNameValuePair;import org.apache.http.util.EntityUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.util.StringUtils;@Servicepublic class HttpClientService {@Autowiredprivate CloseableHttpClient httpClient;@Autowiredprivate RequestConfig requestConfig;//重载几个工具类public String doGet(String url) {return doGet(url,null,null);}public String doGet(String url,Map<String, String> params) {return doGet(url,params,null);}public String doGet(String url, Map<String, String> params, String charset){// 1.检验字符集编码格式charset = StringUtils.isEmpty(charset)?"UTF-8":charset;String result=null;// 2.校验map集合是否为nullif (params != null) {url+="?";for (Map.Entry<String, String> entry : params.entrySet()) {String key = entry.getKey();String value = entry.getValue();url = url + key + "=" + value + "&";}// 经过循环之后多&符url = url.substring(0, url.length() - 1);}HttpGet httpGet = new HttpGet(url);httpGet.setConfig(requestConfig); //设定超时时间try {CloseableHttpResponse response =httpClient.execute(httpGet);if(response.getStatusLine().getStatusCode() == 200) {HttpEntity httpEntity = response.getEntity();result=EntityUtils.toString(httpEntity,charset);}} catch (Exception e) {e.printStackTrace();throw new RuntimeException(e);//转化成运行时异常}return result;}public String doPost(String url,Map<String,String> params,String charset){String result = null;//1.定义请求类型HttpPost post = new HttpPost(url);post.setConfig(requestConfig); //定义超时时间//2.判断字符集是否为nullif(StringUtils.isEmpty(charset)){charset = "UTF-8";}//3.判断用户是否传递参数if(params !=null){//3.2准备List集合信息List<NameValuePair> parameters = new ArrayList<>();//3.3将数据封装到List集合中for (Map.Entry<String,String> entry : params.entrySet()) {parameters.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}//3.1模拟表单提交try {UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters,charset); //采用u8编码//3.4将实体对象封装到请求对象中post.setEntity(formEntity);//get是拼接字符串,post是setEntity} catch (UnsupportedEncodingException e) {e.printStackTrace();}}//4.发送请求try {CloseableHttpResponse response = httpClient.execute(post);//4.1判断返回值状态if(response.getStatusLine().getStatusCode() == 200) {result = EntityUtils.toString(response.getEntity(),charset);}else{System.out.println("获取状态码信息:"+response.getStatusLine().getStatusCode());throw new RuntimeException();}} catch (IOException e) {e.printStackTrace();}return result;}//重载方法public String doPost(String url){return doPost(url, null, null);}public String doPost(String url,Map<String,String> params){return doPost(url, params, null);}public String doPost(String url,String charset){return doPost(url, null, charset);}
}
