|
@@ -1,13 +1,97 @@
|
|
package com.zhentao.lianxi330;
|
|
package com.zhentao.lianxi330;
|
|
|
|
|
|
|
|
+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.boot.SpringApplication;
|
|
import org.springframework.boot.SpringApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
|
|
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
+import java.time.format.DateTimeFormatter;
|
|
|
|
+import java.util.HashMap;
|
|
|
|
+import java.util.Map;
|
|
|
|
+import java.util.UUID;
|
|
|
|
+
|
|
@SpringBootApplication
|
|
@SpringBootApplication
|
|
public class Lianxi330Application {
|
|
public class Lianxi330Application {
|
|
-
|
|
|
|
|
|
+ private static final String BASE_URL = "http://localhost:8080/api";
|
|
|
|
+ private static final String APP_KEY = "your_app_key";
|
|
|
|
+ private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE_TIME;
|
|
public static void main(String[] args) {
|
|
public static void main(String[] args) {
|
|
SpringApplication.run(Lianxi330Application.class, args);
|
|
SpringApplication.run(Lianxi330Application.class, args);
|
|
|
|
+
|
|
|
|
+ postRequest();
|
|
|
|
+ getRequest();
|
|
|
|
+ }
|
|
|
|
+ private static void postRequest() {
|
|
|
|
+ String reqId = UUID.randomUUID().toString();
|
|
|
|
+ String appId = "123456";
|
|
|
|
+ String goodsId = "G001";
|
|
|
|
+ long reqTime = System.currentTimeMillis();
|
|
|
|
+ LocalDateTime nowDate = LocalDateTime.now();
|
|
|
|
+ String nowDateStr = nowDate.format(FORMATTER);
|
|
|
|
+ int amount = 2;
|
|
|
|
+ double price = 99.9;
|
|
|
|
+ String mobile = "13800138000";
|
|
|
|
+
|
|
|
|
+ String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
+ params.put("appId", appId);
|
|
|
|
+ params.put("sign", sign);
|
|
|
|
+ params.put("reqTime", reqTime);
|
|
|
|
+ params.put("goodsId", goodsId);
|
|
|
|
+ params.put("amount", amount);
|
|
|
|
+ params.put("price", price);
|
|
|
|
+ params.put("mobile", mobile);
|
|
|
|
+ params.put("nowDate", nowDateStr);
|
|
|
|
+
|
|
|
|
+ HttpResponse response = HttpRequest.post(BASE_URL + "/post")
|
|
|
|
+ .header("reqId", reqId)
|
|
|
|
+ .header("Content-Type", "application/json")
|
|
|
|
+ .body(JSONUtil.toJsonStr(params))
|
|
|
|
+ .execute();
|
|
|
|
+
|
|
|
|
+ System.out.println("POST Response Status: " + response.getStatus());
|
|
|
|
+ System.out.println("POST Response Body: " + response.body());
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static void getRequest() {
|
|
|
|
+ String reqId = UUID.randomUUID().toString();
|
|
|
|
+ String appId = "123456";
|
|
|
|
+ String goodsId = "G001";
|
|
|
|
+ long reqTime = System.currentTimeMillis();
|
|
|
|
+ LocalDateTime nowDate = LocalDateTime.now();
|
|
|
|
+ String nowDateStr = nowDate.format(FORMATTER);
|
|
|
|
+ int amount = 2;
|
|
|
|
+ double price = 99.9;
|
|
|
|
+ String mobile = "13800138000";
|
|
|
|
+
|
|
|
|
+ String sign = generateSign(appId, goodsId, reqId, reqTime, APP_KEY);
|
|
|
|
+
|
|
|
|
+ Map<String, Object> params = new HashMap<>();
|
|
|
|
+ params.put("appId", appId);
|
|
|
|
+ params.put("sign", sign);
|
|
|
|
+ params.put("reqTime", reqTime);
|
|
|
|
+ params.put("goodsId", goodsId);
|
|
|
|
+ params.put("amount", amount);
|
|
|
|
+ params.put("price", price);
|
|
|
|
+ params.put("mobile", mobile);
|
|
|
|
+ params.put("nowDate", nowDateStr);
|
|
|
|
+
|
|
|
|
+ String queryString = HttpUtil.toParams(params);
|
|
|
|
+ HttpResponse response = HttpRequest.get(BASE_URL + "/get?" + queryString)
|
|
|
|
+ .header("reqId", reqId)
|
|
|
|
+ .execute();
|
|
|
|
+
|
|
|
|
+ System.out.println("GET Response Status: " + response.getStatus());
|
|
|
|
+ System.out.println("GET Response Body: " + response.body());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ private static String generateSign(String appId, String goodsId, String reqId, long reqTime, String appKey) {
|
|
|
|
+ String source = appId + goodsId + reqId + reqTime + appKey;
|
|
|
|
+ return DigestUtil.md5Hex(source);
|
|
|
|
+ }
|
|
}
|
|
}
|