|
@@ -0,0 +1,167 @@
|
|
|
+package cn.feishu.sdk.util;
|
|
|
+
|
|
|
+import cn.hutool.extra.ssh.JschRuntimeException;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.http.client.config.RequestConfig;
|
|
|
+import org.apache.http.client.methods.CloseableHttpResponse;
|
|
|
+import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.entity.ContentType;
|
|
|
+import org.apache.http.entity.StringEntity;
|
|
|
+import org.apache.http.impl.client.CloseableHttpClient;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.URI;
|
|
|
+
|
|
|
+public class HttpUtil {
|
|
|
+
|
|
|
+ public static JSONObject feishuGetRequest(String url, JSONObject params){
|
|
|
+ return HttpUtil.feishuGetRequest(null,url,params);
|
|
|
+ }
|
|
|
+ public static JSONObject feishuPostRequest(String url,JSONObject params){
|
|
|
+ return HttpUtil.feishuPostRequest(null,url,params);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String feishuGetRequestString(String url) {
|
|
|
+ System.out.println(url);
|
|
|
+ // 构造请求
|
|
|
+ HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
|
|
|
+ @Override
|
|
|
+ public String getMethod() {
|
|
|
+ return "GET";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpClient client = null;
|
|
|
+ try {
|
|
|
+ client = HttpClientBuilder.create().build();
|
|
|
+ httpEntity.setURI(URI.create(url));
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
|
|
|
+ httpEntity.setConfig(requestConfig);
|
|
|
+ response = client.execute(httpEntity);
|
|
|
+ if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ System.out.println(result.toString());
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ if (client != null) {
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static JSONObject feishuGetRequest(String accessToken, String url, JSONObject params) {
|
|
|
+ System.out.println(url);
|
|
|
+ // 构造请求
|
|
|
+ HttpEntityEnclosingRequestBase httpEntity = new HttpEntityEnclosingRequestBase() {
|
|
|
+ @Override
|
|
|
+ public String getMethod() {
|
|
|
+ return "GET";
|
|
|
+ }
|
|
|
+ };
|
|
|
+ if(accessToken != null){
|
|
|
+ httpEntity.setHeader("Authorization", accessToken);
|
|
|
+ }
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpClient client = null;
|
|
|
+ try {
|
|
|
+ client = HttpClientBuilder.create().build();
|
|
|
+ httpEntity.setURI(URI.create(url));
|
|
|
+ if (params != null){
|
|
|
+ httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
|
|
|
+ }
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
|
|
|
+ httpEntity.setConfig(requestConfig);
|
|
|
+ response = client.execute(httpEntity);
|
|
|
+ if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ System.out.println(result.toString());
|
|
|
+ return JSONObject.parseObject(result.toString());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ if (client != null) {
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public static JSONObject feishuPostRequest(String accessToken, String url, JSONObject params) {
|
|
|
+ // 构造请求
|
|
|
+ HttpPost httpEntity = new HttpPost(url);
|
|
|
+ if(accessToken != null){
|
|
|
+ httpEntity.setHeader("Authorization", accessToken);
|
|
|
+ }
|
|
|
+ CloseableHttpResponse response = null;
|
|
|
+ CloseableHttpClient client = null;
|
|
|
+ try {
|
|
|
+ client = HttpClientBuilder.create().build();
|
|
|
+ httpEntity.setEntity(new StringEntity(params.toJSONString(), ContentType.APPLICATION_JSON));
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(12000).setConnectTimeout(12000).build();
|
|
|
+ httpEntity.setConfig(requestConfig);
|
|
|
+ response = client.execute(httpEntity);
|
|
|
+ if (response != null && response.getStatusLine().getStatusCode() == 200) {
|
|
|
+ BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ String line = "";
|
|
|
+ while ((line = bufferedReader.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ bufferedReader.close();
|
|
|
+ System.out.println(result.toString());
|
|
|
+ return JSONObject.parseObject(result.toString());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (response != null) {
|
|
|
+ response.close();
|
|
|
+ }
|
|
|
+ if (client != null) {
|
|
|
+ client.close();
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|