package com.landtool.lanbase.common.utils; import java.io.IOException; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSONObject; import cn.hutool.http.HttpUtil; /** * @author * @Description: TODO(获取字典信息) * @date 2018-3-22 15:09 */ public class HttpOperateUtils { /** * HttpGet 请求 URL 返回数据 * * @param url * @return * @throws IOException */ public static String httpGet(String url) throws IOException { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url.trim()); HttpResponse httpResponse = client.execute(request); HttpEntity httpEntity = httpResponse.getEntity(); return EntityUtils.toString(httpEntity, "UTF-8"); } public static boolean isgzip(String url) throws IOException { HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(url.trim()); HttpResponse httpResponse = null; httpResponse = client.execute(request); String Encoding = String.valueOf(httpResponse.getEntity().getContentEncoding()); boolean flag = false; if(Encoding != null && Encoding.indexOf("gzip") > -1) { flag = true; } return flag; } public static T getJsonObject(String url, Class clazz) throws IOException { String resultjson = HttpOperateUtils.httpGet(url); JSONObject jsonobject = JSONObject.parseObject(resultjson); return JSONObject.toJavaObject(jsonobject, clazz); } public static List getJsonObjectArray(String url, Class clazz) throws IOException { String resultjson = HttpOperateUtils.httpGet(url); return JSONObject.parseArray(resultjson, clazz); } public static List getJsonObjectArrayByParams(String url, Class clazz,Map params) throws IOException { String resultjson = HttpUtil.get(url,params); return JSONObject.parseArray(resultjson, clazz); } /** * HttpPost */ public static String httpPost(String url, Object obj) throws IOException { String charset = "utf-8"; HttpClient httpClient = null; HttpPost httpPost = null; String result = null; try { Map map = objectToMap(obj); httpClient = new DefaultHttpClient(); httpPost = new HttpPost(url); // 设置参数 List list = new ArrayList(); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry elem = (Entry) iterator.next(); if(elem.getValue() != null && !elem.getValue().equals("null")) list.add(new BasicNameValuePair(elem.getKey(), elem.getValue())); } if (list.size() > 0) { UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset); httpPost.setEntity(entity); } HttpResponse response = httpClient.execute(httpPost); if (response != null) { HttpEntity resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, charset); } } } catch (Exception ex) { ex.printStackTrace(); } return result; } /** * 获取利用反射获取类里面的值和名称 * * @param obj * @return * @throws IllegalAccessException */ public static Map objectToMap(Object obj) throws IllegalAccessException { Map map = new HashMap<>(); Class clazz = obj.getClass(); System.out.println(clazz); for (Field field : clazz.getDeclaredFields()) { field.setAccessible(true); String fieldName = field.getName(); String value = field.get(obj) != null ? String.valueOf(field.get(obj)) : null; map.put(fieldName, value); } return map; } }