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 APPKEY = "appKey001"; 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 = System.currentTimeMillis()+""; 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 = AppKey.appkey; // String appKey = "111"; String sign = DigestUtil.md5Hex((appId+goodsId+reqId+reqTime+appKey).getBytes()); Map 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("接口二"); HttpRequest response2 = HttpRequest.get(Api2_URL).header("reqId", reqId); for (Map.Entry entry : map.entrySet()) { response2 = response2.form(entry.getKey(), entry.getValue()); } HttpResponse execute = response2.execute(); System.out.println(execute.getStatus()); System.out.println(execute.body()); 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()); } }