|
@@ -1,8 +1,24 @@
|
|
|
package com.test;
|
|
|
|
|
|
-import com.alibaba.fastjson2.JSONArray;
|
|
|
-import com.alibaba.fastjson2.JSONObject;
|
|
|
-import com.jjt.common.utils.http.HttpUtils;
|
|
|
+
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
+import cn.hutool.http.HttpUtil;
|
|
|
+import cn.hutool.http.Method;
|
|
|
+import com.jjt.common.utils.StringUtils;
|
|
|
+import com.jjt.common.utils.sign.Base64;
|
|
|
+import org.yaml.snakeyaml.util.UriEncoder;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.ConnectException;
|
|
|
+import java.net.SocketTimeoutException;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLConnection;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* Test$
|
|
@@ -11,43 +27,68 @@ import com.jjt.common.utils.http.HttpUtils;
|
|
|
* @date 2024/8/22 22:13
|
|
|
*/
|
|
|
public class Test {
|
|
|
- public static void main(String[] args) {
|
|
|
- String url = "http://192.168.188.188:18000/api/sas/diff";
|
|
|
-// jsonObject.put("data", );
|
|
|
- String params = "{\n" +
|
|
|
- " \"data\": [\n" +
|
|
|
- " 55,\n" +
|
|
|
- " 98,\n" +
|
|
|
- " 67,\n" +
|
|
|
- " 20,\n" +
|
|
|
- " 29,\n" +
|
|
|
- " 27,\n" +
|
|
|
- " 59,\n" +
|
|
|
- " 98,\n" +
|
|
|
- " 88,\n" +
|
|
|
- " 27,\n" +
|
|
|
- " 90,\n" +
|
|
|
- " 90,\n" +
|
|
|
- " 94,\n" +
|
|
|
- " 29,\n" +
|
|
|
- " 81,\n" +
|
|
|
- " 77,\n" +
|
|
|
- " 46,\n" +
|
|
|
- " 65,\n" +
|
|
|
- " 35\n" +
|
|
|
- " ]\n" +
|
|
|
- "}";
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(params);
|
|
|
-// String result = HttpUtils.dataPost(url, jsonObject);
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ String uri = "http://192.168.188.66:9090/api/v1/query"; // 目标URL
|
|
|
+ String param = "node_cpu_guest_seconds_total{cpu=\"0\",mode=\"user\"}";
|
|
|
+ param = UriEncoder.encode(param);
|
|
|
+ System.err.println(Base64.encode("admin:123456".getBytes()));
|
|
|
+ String auth = "admin:123456";
|
|
|
+ String baseAuth = "Basic " + Base64.encode(auth.getBytes());
|
|
|
+ Map<String, Object> map = new HashMap<>(16);
|
|
|
+ map.put("query", param);
|
|
|
+ HttpRequest request = HttpUtil.createPost(uri);
|
|
|
+ request.setMethod(Method.GET);
|
|
|
+ request.header("Authorization", baseAuth);
|
|
|
+ request.form(map);
|
|
|
+ request.setConnectionTimeout(5000);
|
|
|
+ HttpResponse execute = request.execute();
|
|
|
+ // 如果操作成功,则不会执行以下代码
|
|
|
+ String res = new String(execute.body().getBytes(), StandardCharsets.UTF_8);
|
|
|
+ System.err.println(res);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static HttpResponse response(HttpRequest request) throws Exception {
|
|
|
+ try (HttpResponse res = request.execute()) {
|
|
|
+ if (!res.isOk()) {
|
|
|
+ throw new RuntimeException(res.body());
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- JSONObject obj = new JSONObject();
|
|
|
- JSONArray array = new JSONArray();
|
|
|
- array.add(1);
|
|
|
- array.add(3);
|
|
|
- array.add(5);
|
|
|
- array.add(7);
|
|
|
- array.add(123);
|
|
|
- obj.put("data", array);
|
|
|
- System.err.println(obj);
|
|
|
+ public static String sendGet(String url, String param, String contentType, String baseAuth) {
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ BufferedReader in = null;
|
|
|
+ try {
|
|
|
+ String urlNameString = StringUtils.isNotBlank(param) ? url + "?" + param : url;
|
|
|
+ URL realUrl = new URL(urlNameString);
|
|
|
+ URLConnection connection = realUrl.openConnection();
|
|
|
+ connection.setRequestProperty("Authorization", baseAuth);
|
|
|
+ connection.setRequestProperty("accept", "*/*");
|
|
|
+ connection.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
|
|
+ connection.connect();
|
|
|
+ in = new BufferedReader(new InputStreamReader(connection.getInputStream(), contentType));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null) {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ } catch (ConnectException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (SocketTimeoutException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if (in != null) {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
}
|
|
|
}
|