116 lines
3.9 KiB
Java
116 lines
3.9 KiB
Java
package com.muyu.test;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.InputStreamReader;
|
|
import java.net.HttpURLConnection;
|
|
import java.net.URL;
|
|
import java.util.HashMap;
|
|
import java.util.Map;
|
|
|
|
/**
|
|
* @author yn
|
|
* @description TODO
|
|
* @date 2024-04-13 9:39
|
|
*/
|
|
public class fluxmqTest {
|
|
public static void main(String[] args) {
|
|
// FluxMQ 注册接口 URL
|
|
String fluxMQRegisterUrl = "http://43.143.161.183/register";
|
|
|
|
// 第一个请求获取 token 的 URL
|
|
String getTokenUrl = "http://43.143.161.183/getToken";
|
|
|
|
try {
|
|
// 发起第一个请求获取 token
|
|
String token = getToken(getTokenUrl);
|
|
|
|
// 构建注册请求参数,包括 token
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("username", "your_username");
|
|
params.put("email", "your_email@example.com");
|
|
params.put("token", token);
|
|
|
|
// 发起注册请求
|
|
String response = register(fluxMQRegisterUrl, params);
|
|
|
|
// 处理注册响应结果
|
|
System.out.println("Response from FluxMQ registration: " + response);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
// 发起第一个请求获取 token
|
|
private static String getToken(String url) throws Exception {
|
|
HttpURLConnection conn = null;
|
|
try {
|
|
URL tokenUrl = new URL(url);
|
|
conn = (HttpURLConnection) tokenUrl.openConnection();
|
|
conn.setRequestMethod("GET");
|
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
String line;
|
|
StringBuilder response = new StringBuilder();
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
|
|
// 解析 JSON 响应获取 token
|
|
// 这里假设 JSON 响应格式为 {"token": "your_token_value"}
|
|
String jsonResponse = response.toString();
|
|
return jsonResponse.substring(jsonResponse.indexOf(':') + 2, jsonResponse.lastIndexOf('"'));
|
|
} finally {
|
|
if (conn != null) {
|
|
conn.disconnect();
|
|
}
|
|
}
|
|
}
|
|
|
|
// 发起注册请求
|
|
private static String register(String url, Map<String, String> params) throws Exception {
|
|
HttpURLConnection conn = null;
|
|
try {
|
|
URL registerUrl = new URL(url);
|
|
conn = (HttpURLConnection) registerUrl.openConnection();
|
|
|
|
// 设置请求方法为 POST
|
|
conn.setRequestMethod("POST");
|
|
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
|
|
conn.setRequestProperty("Accept", "application/json");
|
|
conn.setDoInput(true);
|
|
conn.setDoOutput(true);
|
|
|
|
// 构建请求参数
|
|
StringBuilder postData = new StringBuilder();
|
|
for (Map.Entry<String, String> param : params.entrySet()) {
|
|
if (postData.length() != 0) {
|
|
postData.append('&');
|
|
}
|
|
postData.append(param.getKey());
|
|
postData.append('=');
|
|
postData.append(param.getValue());
|
|
}
|
|
|
|
// 将参数写入请求体
|
|
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
|
|
conn.getOutputStream().write(postDataBytes);
|
|
|
|
// 获取注册响应
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
|
|
String line;
|
|
StringBuilder response = new StringBuilder();
|
|
while ((line = reader.readLine()) != null) {
|
|
response.append(line);
|
|
}
|
|
reader.close();
|
|
|
|
return response.toString();
|
|
} finally {
|
|
if (conn != null) {
|
|
conn.disconnect();
|
|
}
|
|
}
|
|
}
|
|
}
|