Utils--前台调用后台接口工具类
package com.taotao.manage.httpclient; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.NameValuePair; import org.apache.http.ParseException; 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.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.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ApiHttpClientService implements BeanFactoryAware { private BeanFactory beanFactory; @Autowired(required=false) private RequestConfig config; /** * GET请求,成功返回String;失败返回null * * @param url * @return * @throws ParseException * @throws IOException */ public String doGet(String url) throws ParseException, IOException { // 2、创建http GET请求 HttpGet httpGet = new HttpGet(url); httpGet.setConfig(config); CloseableHttpResponse response = null; try { // 3、执行请求 response = getttpClient().execute(httpGet); // 4、判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } finally { if (response != null) { response.close(); } } return null; } /** * 带有参数的GET请求 ,返回:null,请求失败,String数据,请求成功 * * @param url * @param params * @return * @throws URISyntaxException * @throws IOException * @throws ParseException */ public String doGet(String url, Map<String, String> params) throws URISyntaxException, ParseException, IOException { URIBuilder uriBuilder = new URIBuilder(url); for (Map.Entry<String, String> map : params.entrySet()) { uriBuilder.setParameter(map.getKey(), map.getValue()); } return this.doGet(uriBuilder.build().toURL().toString()); } /** * 创建http POST请求 * * @param url * @param params * @return * @throws IOException * @throws ParseException */ public HttpResult doPost(String url, Map<String, String> params) throws ParseException, IOException { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(config); // 伪装成浏览器访问 // 设置2个post参数,一个是scope、一个是q if (params != null) { List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); for (Map.Entry<String, String> map : params.entrySet()) { parameters.add(new BasicNameValuePair(map.getKey(), map.getValue())); } // 构造一个form表单式的实体 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); // 将请求实体设置到httpPost对象中 httpPost.setEntity(formEntity); } CloseableHttpResponse response = null; try { // 执行请求 response = getttpClient().execute(httpPost); // 判断返回状态是否为200 return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } finally { if (response != null) { response.close(); } } } /** * POST无餐请求 * * @param url * @return * @throws ParseException * @throws IOException */ public HttpResult doPost(String url) throws ParseException, IOException { return this.doPost(url, null); } /** * 拿到最新的,是多例的 * * @return */ public CloseableHttpClient getttpClient() { return this.beanFactory.getBean(CloseableHttpClient.class); } /** * spring/applicationContext-HttpClient为多例 创建多例 ,可以放对象 * * @param bean * @throws BeansException */ @Override public void setBeanFactory(BeanFactory bean) throws BeansException { this.beanFactory = bean; } /** * 创建http POST请求json * * @param url * @param params * @return * @throws IOException * @throws ParseException */ public HttpResult doPostJSON(String url, String params) throws ParseException, IOException { HttpPost httpPost = new HttpPost(url); httpPost.setConfig(config); // 伪装成浏览器访问 // 设置2个post参数,一个是scope、一个是q if (params != null) { StringEntity entity=new StringEntity(params, ContentType.APPLICATION_JSON); // 将请求实体设置到httpPost对象中 httpPost.setEntity(entity); } CloseableHttpResponse response = null; try { // 执行请求 response = getttpClient().execute(httpPost); // 判断返回状态是否为200 return new HttpResult(response.getStatusLine().getStatusCode(), EntityUtils.toString(response.getEntity(), "UTF-8")); } finally { if (response != null) { response.close(); } } } }
好久之前写的,今天忽然有个想法,把这些工具类总计下。以便以后的使用