Browse Source

郭丽娜作业2

36052 3 months ago
parent
commit
2528eac8cd
1 changed files with 83 additions and 0 deletions
  1. 83 0
      src/main/java/com/zhentao/ApiClient.java

+ 83 - 0
src/main/java/com/zhentao/ApiClient.java

@@ -0,0 +1,83 @@
+package com.zhentao;
+
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.crypto.digest.DigestUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONUtil;
+import org.springframework.util.DigestUtils;
+
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.UUID;
+
+public class ApiClient {
+    //1、自已写一个main方法去调用这两个接口,用hutool工具类,r
+    //
+    //2、main方法写md5加密生成签名,加密规则appId+goodsId+reqId+reqTime+appKey
+    //接口一与接口二验签方式md5()签名与验证签名的规则appId+goodsId+reqId+reqTime+appKey生成md5串,去签验。
+    //"appId": "APP123",
+    //    "sign": "SIGNATURE",
+    //    "reqTime": "1679999999",
+    //    "goodsId": "GOODS001",
+    //    "amount": 2,
+    //    "price": 100.5,
+    //    "mobile": "13812345678",
+    //    "nowDate": "2025-03-29"
+
+    private static final String Api1_URL = "http://127.0.0.1:8080/api/api1";
+    private static final String Api2_URL = "http://127.0.0.1:8080/api/api2";
+    public static void main(String[] args) {
+        //"appId": "APP123",
+        //    "sign": "SIGNATURE",
+        //    "reqTime": "1679999999",
+        //    "goodsId": "GOODS001",
+        //    "amount": 2,
+        //    "price": 100.5,
+        //    "mobile": "13812345678",
+        //    "nowDate": "2025-03-29"
+        System.out.println("接口一");
+        String appId = "APP123";
+        String reqTime = "1679999999";
+        String goodsId = "GOODS001";
+        Integer amount = 2;
+        Double price = 99.9;
+        String mobile = "13812345678";
+        String nowDate = DateUtil.format(new Date(), "yyyy-MM-dd");
+        String reqId = "reqId001";
+        String appKey = "appKey001";
+        String sign = DigestUtil.md5Hex((appId+goodsId+reqId+reqTime+appKey).getBytes());
+        Map<String, Object> map = new HashMap<>();
+        map.put("appId", appId);
+        map.put("sign", sign);
+        map.put("reqTime", reqTime);
+        map.put("goodsId", goodsId);
+        map.put("amount", amount);
+        map.put("price", price);
+        map.put("mobile", mobile);
+        map.put("nowDate", nowDate);
+//        map.put("reqId", reqId);
+        String s = JSONUtil.toJsonStr(map);
+        HttpResponse response = HttpRequest.post(Api1_URL).header("reqId", reqId).body(s).execute();
+        System.out.println(response.getStatus());
+        System.out.println(response.body());
+        System.out.println("----------------------------------------------");
+        System.out.println("接口二");
+        String url = Api2_URL + "?appId=" + appId +
+                "&sign=" + sign +
+                "&reqTime=" + reqTime +
+                "&goodsId=" + goodsId +
+                "&amount=" + amount +
+                "&price=" + price +
+                "&mobile=" + mobile +
+                "&nowDate=" + nowDate;
+        System.out.println(url);
+        HttpResponse response1 = HttpUtil.createGet(url).header("reqId", reqId).execute();
+        System.out.println(response1.getStatus());
+        System.out.println(response1.body());
+    }
+
+}