123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- 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;
- }
- }
|