|
@@ -0,0 +1,122 @@
|
|
|
|
+package cn.zhentao;
|
|
|
|
+
|
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
|
+import cn.hutool.http.HttpResponse;
|
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
|
+
|
|
|
|
+import java.math.BigDecimal;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Date;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+
|
|
|
|
+public class Test {
|
|
|
|
+
|
|
|
|
+ // 配置参数(需替换为实际值)
|
|
|
|
+ private static final String BASE_URL = "http://localhost:8080/api";
|
|
|
|
+ private static final String APP_KEY = "1234567890";
|
|
|
|
+ private static final String APP_ID = "test_app";
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ // 调用接口一(POST请求)
|
|
|
|
+ callInterfaceOne();
|
|
|
|
+
|
|
|
|
+ // 调用接口二(GET请求)
|
|
|
|
+ callInterfaceTwo();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 调用接口一(POST /interfaceOne)
|
|
|
|
+ */
|
|
|
|
+ private static void callInterfaceOne() {
|
|
|
|
+ // 请求参数
|
|
|
|
+ String reqId = "req_" + System.currentTimeMillis();
|
|
|
|
+ Long goodsId = 10001L;
|
|
|
|
+ Long reqTime = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ // 生成签名
|
|
|
|
+ String sign = generateMd5Sign(APP_ID, goodsId, reqId, reqTime, APP_KEY);
|
|
|
|
+ System.err.println(sign);
|
|
|
|
+ // 构造请求体
|
|
|
|
+ Map<String, Object> requestBody = new HashMap<String, Object>();
|
|
|
|
+ requestBody.put("appId", APP_ID);
|
|
|
|
+ requestBody.put("sign", sign);
|
|
|
|
+ requestBody.put("goodsId", goodsId);
|
|
|
|
+ requestBody.put("reqTime", reqTime);
|
|
|
|
+ requestBody.put("amount", 2);
|
|
|
|
+ requestBody.put("price", new BigDecimal("199.99"));
|
|
|
|
+ requestBody.put("mobile", "13800138000");
|
|
|
|
+ requestBody.put("nowDate", new Date());
|
|
|
|
+
|
|
|
|
+ // 请求头
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("reqId", reqId);
|
|
|
|
+ headers.put("Content-Type", "application/json");
|
|
|
|
+
|
|
|
|
+ // 发送请求
|
|
|
|
+ HttpResponse response = HttpRequest.post(BASE_URL + "/interfaceOne")
|
|
|
|
+ .addHeaders(headers)
|
|
|
|
+ .body(JSONUtil.toJsonStr(requestBody))
|
|
|
|
+ .execute();
|
|
|
|
+
|
|
|
|
+ System.out.println("=== 接口一调用结果 ===");
|
|
|
|
+ System.out.println("状态码: " + response.getStatus());
|
|
|
|
+ System.out.println("响应体: " + response.body());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 调用接口二(GET /interfaceTwo)
|
|
|
|
+ */
|
|
|
|
+ private static void callInterfaceTwo() {
|
|
|
|
+ // 请求参数
|
|
|
|
+ String reqId = "req_" + (System.currentTimeMillis() + 1);
|
|
|
|
+ Long goodsId = 10002L;
|
|
|
|
+ Long reqTime = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ // 生成签名
|
|
|
|
+ String sign = generateMd5Sign(APP_ID, goodsId, reqId, reqTime, APP_KEY);
|
|
|
|
+
|
|
|
|
+ // GET请求参数
|
|
|
|
+ Map<String, Object> params = new HashMap<String, Object>();
|
|
|
|
+ params.put("appId", APP_ID);
|
|
|
|
+ params.put("sign", sign);
|
|
|
|
+ params.put("goodsId", goodsId);
|
|
|
|
+ params.put("reqTime", reqTime);
|
|
|
|
+ params.put("amount", 1);
|
|
|
|
+ params.put("price", new BigDecimal("99.50"));
|
|
|
|
+ params.put("mobile", "13900139000");
|
|
|
|
+ params.put("nowDate", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
|
|
|
|
+
|
|
|
|
+ // 请求头
|
|
|
|
+ Map<String, String> headers = new HashMap<String, String>();
|
|
|
|
+ headers.put("reqId", reqId);
|
|
|
|
+
|
|
|
|
+ // 发送请求
|
|
|
|
+ HttpResponse response = HttpRequest.get(BASE_URL + "/interfaceTwo")
|
|
|
|
+ .addHeaders(headers)
|
|
|
|
+ .form( params)
|
|
|
|
+ .execute();
|
|
|
|
+
|
|
|
|
+ System.out.println("\n=== 接口二调用结果 ===");
|
|
|
|
+ System.out.println("状态码: " + response.getStatus());
|
|
|
|
+ System.out.println("响应体: " + response.body());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成MD5签名
|
|
|
|
+ */
|
|
|
|
+ private static String generateMd5Sign(String appId, Long goodsId, String reqId, Long reqTime, String appKey) {
|
|
|
|
+ String rawString = appId + goodsId + reqId + reqTime + appKey;
|
|
|
|
+ return cn.hutool.crypto.digest.DigestUtil.md5Hex(rawString);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 验证签名(模拟服务端返回的签名验证)
|
|
|
|
+ */
|
|
|
|
+ private static boolean verifySign(String responseBody, String appId, Long goodsId, String reqId, Long reqTime, String appKey) {
|
|
|
|
+ // 实际项目中需要解析响应体获取服务端返回的签名
|
|
|
|
+ // 这里模拟验证过程
|
|
|
|
+ String expectedSign = generateMd5Sign(appId, goodsId, reqId, reqTime, appKey);
|
|
|
|
+ return responseBody.contains(expectedSign.substring(0, 8)); // 简单验证前8位
|
|
|
|
+ }
|
|
|
|
+}
|