ApiClient.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package com.zhentao;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.crypto.digest.DigestUtil;
  4. import cn.hutool.http.HttpRequest;
  5. import cn.hutool.http.HttpResponse;
  6. import cn.hutool.http.HttpUtil;
  7. import cn.hutool.json.JSONUtil;
  8. import org.springframework.util.DigestUtils;
  9. import java.util.Date;
  10. import java.util.HashMap;
  11. import java.util.Map;
  12. import java.util.UUID;
  13. public class ApiClient {
  14. //1、自已写一个main方法去调用这两个接口,用hutool工具类,r
  15. //
  16. //2、main方法写md5加密生成签名,加密规则appId+goodsId+reqId+reqTime+appKey
  17. //接口一与接口二验签方式md5()签名与验证签名的规则appId+goodsId+reqId+reqTime+appKey生成md5串,去签验。
  18. //"appId": "APP123",
  19. // "sign": "SIGNATURE",
  20. // "reqTime": "1679999999",
  21. // "goodsId": "GOODS001",
  22. // "amount": 2,
  23. // "price": 100.5,
  24. // "mobile": "13812345678",
  25. // "nowDate": "2025-03-29"
  26. // private static final String APPKEY = "appKey001";
  27. private static final String Api1_URL = "http://127.0.0.1:8080/api/api1";
  28. private static final String Api2_URL = "http://127.0.0.1:8080/api/api2";
  29. public static void main(String[] args) {
  30. //"appId": "APP123",
  31. // "sign": "SIGNATURE",
  32. // "reqTime": "1679999999",
  33. // "goodsId": "GOODS001",
  34. // "amount": 2,
  35. // "price": 100.5,
  36. // "mobile": "13812345678",
  37. // "nowDate": "2025-03-29"
  38. System.out.println("接口一");
  39. String appId = "APP123";
  40. String reqTime = System.currentTimeMillis()+"";
  41. String goodsId = "GOODS001";
  42. Integer amount = 2;
  43. Double price = 99.9;
  44. String mobile = "13812345678";
  45. String nowDate = DateUtil.format(new Date(), "yyyy-MM-dd");
  46. String reqId = "reqId001";
  47. String appKey = AppKey.appkey;
  48. // String appKey = "111";
  49. String sign = DigestUtil.md5Hex((appId+goodsId+reqId+reqTime+appKey).getBytes());
  50. Map<String, Object> map = new HashMap<>();
  51. map.put("appId", appId);
  52. map.put("sign", sign);
  53. map.put("reqTime", reqTime);
  54. map.put("goodsId", goodsId);
  55. map.put("amount", amount);
  56. map.put("price", price);
  57. map.put("mobile", mobile);
  58. map.put("nowDate", nowDate);
  59. // map.put("reqId", reqId);
  60. String s = JSONUtil.toJsonStr(map);
  61. HttpResponse response = HttpRequest.post(Api1_URL).header("reqId", reqId).body(s).execute();
  62. System.out.println(response.getStatus());
  63. System.out.println(response.body());
  64. System.out.println("----------------------------------------------");
  65. System.out.println("接口二");
  66. HttpRequest response2 = HttpRequest.get(Api2_URL).header("reqId", reqId);
  67. for (Map.Entry<String, Object> entry : map.entrySet()) {
  68. response2 = response2.form(entry.getKey(), entry.getValue());
  69. }
  70. HttpResponse execute = response2.execute();
  71. System.out.println(execute.getStatus());
  72. System.out.println(execute.body());
  73. System.out.println("----------------------------------------------");
  74. String url = Api2_URL + "?appId=" + appId +
  75. "&sign=" + sign +
  76. "&reqTime=" + reqTime +
  77. "&goodsId=" + goodsId +
  78. "&amount=" + amount +
  79. "&price=" + price +
  80. "&mobile=" + mobile +
  81. "&nowDate=" + nowDate;
  82. System.out.println(url);
  83. HttpResponse response1 = HttpUtil.createGet(url).header("reqId", reqId).execute();
  84. System.out.println(response1.getStatus());
  85. System.out.println(response1.body());
  86. }
  87. }