ZkhHttpUtil.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * CopyRight © 2018 ZKH All Rights Reserved.
  3. */
  4. package com.zkh360.core.util;
  5. import lombok.extern.slf4j.Slf4j;
  6. import net.sf.json.JSONArray;
  7. import net.sf.json.JSONObject;
  8. import net.sf.json.JsonConfig;
  9. import net.sf.json.processors.DefaultDefaultValueProcessor;
  10. import org.apache.http.HttpEntity;
  11. import org.apache.http.HttpException;
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.methods.*;
  14. import org.apache.http.entity.StringEntity;
  15. import org.apache.http.impl.client.CloseableHttpClient;
  16. import org.apache.http.message.BasicHeader;
  17. import org.apache.http.util.EntityUtils;
  18. import org.springframework.stereotype.Component;
  19. import javax.annotation.Resource;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. @Slf4j
  24. @Component
  25. public class ZkhHttpUtil {
  26. @Resource(name = "httpClientManager")
  27. private CloseableHttpClient client;
  28. JsonConfig jsonConfig = new JsonConfig();
  29. public String requestWithJSON(String method,String newApi, Map<String, Object> params) throws Exception {
  30. CloseableHttpResponse response = null;
  31. jsonConfig.registerDefaultValueProcessor(Double.class,new DefaultDefaultValueProcessor(){
  32. @Override
  33. public Object getDefaultValue(Class type) {
  34. return null;
  35. }});
  36. JSONObject jsonObject = JSONObject.fromObject(params,jsonConfig);
  37. String json = jsonObject.toString();
  38. if(method.equals("POST")){
  39. HttpPost httpPost = new HttpPost(GWdomain.ZKHWebsiteUrl.concat(newApi));
  40. httpPost.addHeader("Content-Type", "application/json");
  41. httpPost.setEntity(setPostOrPutRequestHttpContent(json));
  42. log.info("POST请求官网的uri:"+httpPost.getURI());
  43. log.info("POST请求官网的param:"+json);
  44. response = client.execute(httpPost);
  45. } else if (method.equals("GET")) {
  46. StringBuffer sb = getOrDeleteRequestParams(newApi,params);
  47. log.info("GET请求官网的地址:"+sb.toString());
  48. HttpGet httpGet = new HttpGet(sb.toString());
  49. response = client.execute(httpGet);
  50. }else if(method.equals("PUT")){
  51. HttpPut httpPut = new HttpPut(GWdomain.ZKHWebsiteUrl.concat(newApi));
  52. httpPut.addHeader("Content-Type", "application/json");
  53. httpPut.setEntity(setPostOrPutRequestHttpContent(json));
  54. log.info("PUT请求官网uri:"+httpPut.getURI());
  55. log.info("PUT请求官网param:"+json);
  56. response = client.execute(httpPut);
  57. }else if(method.equals("DELETE")){
  58. StringBuffer sb = getOrDeleteRequestParams(newApi,params);
  59. log.info("DELETE请求官网的地址:"+sb.toString());
  60. HttpDelete httpDelete = new HttpDelete(sb.toString());
  61. response = client.execute(httpDelete);
  62. }
  63. return judgeResult(response);
  64. }
  65. public String postRequestWithJSONArray(String newApi, List list) throws Exception {
  66. HttpPost httpPost = new HttpPost(GWdomain.ZKHWebsiteUrl.concat(newApi));
  67. httpPost.addHeader("Content-Type", "application/json");
  68. return doPostWithJSONArray(list, httpPost);
  69. }
  70. private static StringBuffer getOrDeleteRequestParams(String newApi, Map<String, Object> params){
  71. StringBuffer sb = new StringBuffer(GWdomain.ZKHWebsiteUrl.concat(newApi));
  72. Iterator<Map.Entry<String,Object>> it = params.entrySet().iterator();
  73. if(params != null || !params.isEmpty()){
  74. while (it.hasNext())
  75. {
  76. Map.Entry<String,Object> entry = it.next();
  77. Object key = entry.getKey();
  78. sb.append("&");
  79. sb.append(key);
  80. sb.append('=');
  81. Object value = entry.getValue();
  82. if(isArray(value)){
  83. Integer[] value1 = (Integer[]) value;
  84. for(int i = 0; i < value1.length; i++){
  85. sb.append(value1[i]);
  86. if(i == value1.length -1){
  87. break;
  88. }
  89. sb.append("&");
  90. sb.append(key);
  91. sb.append('=');
  92. }
  93. }else{
  94. sb.append(value);
  95. }
  96. if (!it.hasNext())
  97. {
  98. break;
  99. }
  100. }
  101. }
  102. return sb;
  103. }
  104. private static boolean isArray(Object obj) {
  105. if(obj == null) {
  106. return false;
  107. }
  108. return obj.getClass().isArray();
  109. }
  110. private String doPostWithJSONArray(List list, HttpPost httpPost) throws Exception {
  111. JSONArray jsonArray = new JSONArray();
  112. if(list != null && list.size() > 0) {
  113. jsonArray = JSONArray.fromObject(list);
  114. }
  115. httpPost.setEntity(setPostOrPutRequestHttpContent(jsonArray.toString()));
  116. log.info("POST请求官网uri:"+httpPost.getURI());
  117. log.info("POST请求官网param:"+jsonArray.toString());
  118. HttpResponse response = client.execute(httpPost);
  119. return judgeResult(response);
  120. }
  121. private static StringEntity setPostOrPutRequestHttpContent(String param){
  122. StringEntity se = new StringEntity(param, "UTF-8");
  123. se.setContentType("text/json");
  124. se.setContentEncoding(new BasicHeader("Content-Type", "application/json"));
  125. return se;
  126. }
  127. private String judgeResult(HttpResponse response)throws Exception{
  128. String result = null;
  129. HttpEntity resEntity = null;
  130. try{
  131. if (response != null) {
  132. resEntity = response.getEntity();
  133. if (resEntity != null) {
  134. result = EntityUtils.toString(resEntity, "UTF-8");
  135. }
  136. }
  137. }catch (Exception e){
  138. throw new HttpException("Http请求处理失败", e);
  139. }finally {
  140. closeHttpConnect(resEntity);
  141. }
  142. return result;
  143. }
  144. /**
  145. * 释放连接
  146. * @param response
  147. */
  148. private void closeHttpConnect(HttpEntity response) {
  149. if(null != response){
  150. EntityUtils.consumeQuietly(response);
  151. }
  152. }
  153. }