HttpUtil.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package cn.feishu.sdk.util;
  2. import cn.hutool.extra.ssh.JschRuntimeException;
  3. import com.alibaba.fastjson.JSONObject;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.client.methods.CloseableHttpResponse;
  6. import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
  7. import org.apache.http.client.methods.HttpPost;
  8. import org.apache.http.entity.ContentType;
  9. import org.apache.http.entity.StringEntity;
  10. import org.apache.http.impl.client.CloseableHttpClient;
  11. import org.apache.http.impl.client.HttpClientBuilder;
  12. import java.io.BufferedReader;
  13. import java.io.IOException;
  14. import java.io.InputStreamReader;
  15. import java.net.URI;
  16. public class HttpUtil {
  17. public static JSONObject feishuGetRequest(String url, JSONObject params){
  18. return HttpUtil.feishuGetRequest(null,url,params);
  19. }
  20. public static JSONObject feishuPostRequest(String url,JSONObject params){
  21. return HttpUtil.feishuPostRequest(null,url,params);
  22. }
  23. public static String feishuGetRequestString(String url) {
  24. System.out.println(url);
  25. // 构造请求
  26. HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
  27. @Override
  28. public String getMethod() {
  29. return "GET";
  30. }
  31. };
  32. CloseableHttpResponse response = null;
  33. CloseableHttpClient client = null;
  34. try {
  35. client = HttpClientBuilder.create().build();
  36. httpEntity.setURI(URI.create(url));
  37. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
  38. httpEntity.setConfig(requestConfig);
  39. response = client.execute(httpEntity);
  40. if (response != null && response.getStatusLine().getStatusCode() == 200) {
  41. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  42. StringBuilder result = new StringBuilder();
  43. String line = "";
  44. while ((line = bufferedReader.readLine()) != null) {
  45. result.append(line);
  46. }
  47. bufferedReader.close();
  48. System.out.println(result.toString());
  49. return result.toString();
  50. }
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. } finally {
  54. try {
  55. if (response != null) {
  56. response.close();
  57. }
  58. if (client != null) {
  59. client.close();
  60. }
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. }
  64. }
  65. return null;
  66. }
  67. public static JSONObject feishuGetRequest(String accessToken, String url, JSONObject params) {
  68. System.out.println(url);
  69. // 构造请求
  70. HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
  71. @Override
  72. public String getMethod() {
  73. return "GET";
  74. }
  75. };
  76. if(accessToken != null){
  77. httpEntity.setHeader("Authorization", accessToken);
  78. }
  79. CloseableHttpResponse response = null;
  80. CloseableHttpClient client = null;
  81. try {
  82. client = HttpClientBuilder.create().build();
  83. httpEntity.setURI(URI.create(url));
  84. if (params != null){
  85. httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
  86. }
  87. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
  88. httpEntity.setConfig(requestConfig);
  89. response = client.execute(httpEntity);
  90. if (response != null && response.getStatusLine().getStatusCode() == 200) {
  91. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  92. StringBuilder result = new StringBuilder();
  93. String line = "";
  94. while ((line = bufferedReader.readLine()) != null) {
  95. result.append(line);
  96. }
  97. bufferedReader.close();
  98. System.out.println(result.toString());
  99. return JSONObject.parseObject(result.toString());
  100. }
  101. } catch (Exception e) {
  102. e.printStackTrace();
  103. } finally {
  104. try {
  105. if (response != null) {
  106. response.close();
  107. }
  108. if (client != null) {
  109. client.close();
  110. }
  111. } catch (IOException e) {
  112. e.printStackTrace();
  113. }
  114. }
  115. return null;
  116. }
  117. public static JSONObject feishuPostRequest(String accessToken, String url, JSONObject params) {
  118. // 构造请求
  119. HttpPost httpEntity = new HttpPost(url);
  120. if(accessToken != null){
  121. httpEntity.setHeader("Authorization", accessToken);
  122. }
  123. CloseableHttpResponse response = null;
  124. CloseableHttpClient client = null;
  125. try {
  126. client = HttpClientBuilder.create().build();
  127. httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
  128. RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
  129. httpEntity.setConfig(requestConfig);
  130. response = client.execute(httpEntity);
  131. if (response != null && response.getStatusLine().getStatusCode() == 200) {
  132. BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  133. StringBuilder result = new StringBuilder();
  134. String line = "";
  135. while ((line = bufferedReader.readLine()) != null) {
  136. result.append(line);
  137. }
  138. bufferedReader.close();
  139. System.out.println(result.toString());
  140. return JSONObject.parseObject(result.toString());
  141. }
  142. } catch (Exception e) {
  143. e.printStackTrace();
  144. } finally {
  145. try {
  146. if (response != null) {
  147. response.close();
  148. }
  149. if (client != null) {
  150. client.close();
  151. }
  152. } catch (IOException e) {
  153. e.printStackTrace();
  154. }
  155. }
  156. return null;
  157. }
  158. }