/* * CopyRight © 2018 ZKH All Rights Reserved. */ package com.zkh360.core.util; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONArray; import net.sf.json.JSONObject; import net.sf.json.JsonConfig; import net.sf.json.processors.DefaultDefaultValueProcessor; import org.apache.http.HttpEntity; import org.apache.http.HttpException; import org.apache.http.HttpResponse; import org.apache.http.client.methods.*; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.Iterator; import java.util.List; import java.util.Map; @Slf4j @Component public class ZkhHttpUtil { @Resource(name = "httpClientManager") private CloseableHttpClient client; JsonConfig jsonConfig = new JsonConfig(); public String requestWithJSON(String method,String newApi, Map params) throws Exception { CloseableHttpResponse response = null; jsonConfig.registerDefaultValueProcessor(Double.class,new DefaultDefaultValueProcessor(){ @Override public Object getDefaultValue(Class type) { return null; }}); JSONObject jsonObject = JSONObject.fromObject(params,jsonConfig); String json = jsonObject.toString(); if(method.equals("POST")){ HttpPost httpPost = new HttpPost(GWdomain.ZKHWebsiteUrl.concat(newApi)); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(setPostOrPutRequestHttpContent(json)); log.info("POST请求官网的uri:"+httpPost.getURI()); log.info("POST请求官网的param:"+json); response = client.execute(httpPost); } else if (method.equals("GET")) { StringBuffer sb = getOrDeleteRequestParams(newApi,params); log.info("GET请求官网的地址:"+sb.toString()); HttpGet httpGet = new HttpGet(sb.toString()); response = client.execute(httpGet); }else if(method.equals("PUT")){ HttpPut httpPut = new HttpPut(GWdomain.ZKHWebsiteUrl.concat(newApi)); httpPut.addHeader("Content-Type", "application/json"); httpPut.setEntity(setPostOrPutRequestHttpContent(json)); log.info("PUT请求官网uri:"+httpPut.getURI()); log.info("PUT请求官网param:"+json); response = client.execute(httpPut); }else if(method.equals("DELETE")){ StringBuffer sb = getOrDeleteRequestParams(newApi,params); log.info("DELETE请求官网的地址:"+sb.toString()); HttpDelete httpDelete = new HttpDelete(sb.toString()); response = client.execute(httpDelete); } return judgeResult(response); } public String postRequestWithJSONArray(String newApi, List list) throws Exception { HttpPost httpPost = new HttpPost(GWdomain.ZKHWebsiteUrl.concat(newApi)); httpPost.addHeader("Content-Type", "application/json"); return doPostWithJSONArray(list, httpPost); } private static StringBuffer getOrDeleteRequestParams(String newApi, Map params){ StringBuffer sb = new StringBuffer(GWdomain.ZKHWebsiteUrl.concat(newApi)); Iterator> it = params.entrySet().iterator(); if(params != null || !params.isEmpty()){ while (it.hasNext()) { Map.Entry entry = it.next(); Object key = entry.getKey(); sb.append("&"); sb.append(key); sb.append('='); Object value = entry.getValue(); if(isArray(value)){ Integer[] value1 = (Integer[]) value; for(int i = 0; i < value1.length; i++){ sb.append(value1[i]); if(i == value1.length -1){ break; } sb.append("&"); sb.append(key); sb.append('='); } }else{ sb.append(value); } if (!it.hasNext()) { break; } } } return sb; } private static boolean isArray(Object obj) { if(obj == null) { return false; } return obj.getClass().isArray(); } private String doPostWithJSONArray(List list, HttpPost httpPost) throws Exception { JSONArray jsonArray = new JSONArray(); if(list != null && list.size() > 0) { jsonArray = JSONArray.fromObject(list); } httpPost.setEntity(setPostOrPutRequestHttpContent(jsonArray.toString())); log.info("POST请求官网uri:"+httpPost.getURI()); log.info("POST请求官网param:"+jsonArray.toString()); HttpResponse response = client.execute(httpPost); return judgeResult(response); } private static StringEntity setPostOrPutRequestHttpContent(String param){ StringEntity se = new StringEntity(param, "UTF-8"); se.setContentType("text/json"); se.setContentEncoding(new BasicHeader("Content-Type", "application/json")); return se; } private String judgeResult(HttpResponse response)throws Exception{ String result = null; HttpEntity resEntity = null; try{ if (response != null) { resEntity = response.getEntity(); if (resEntity != null) { result = EntityUtils.toString(resEntity, "UTF-8"); } } }catch (Exception e){ throw new HttpException("Http请求处理失败", e); }finally { closeHttpConnect(resEntity); } return result; } /** * 释放连接 * @param response */ private void closeHttpConnect(HttpEntity response) { if(null != response){ EntityUtils.consumeQuietly(response); } } }