5 changed files with 116 additions and 11 deletions
@ -0,0 +1,99 @@ |
|||||
|
package com.zdxt.enforcementcode.common.util; |
||||
|
|
||||
|
import cn.hutool.core.util.IdUtil; |
||||
|
import cn.hutool.http.HttpRequest; |
||||
|
import cn.hutool.http.HttpResponse; |
||||
|
import com.alibaba.fastjson2.JSONObject; |
||||
|
|
||||
|
import java.util.HashMap; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
public class ApiSignDemo { |
||||
|
|
||||
|
private static final String APP_KEY = "4263693515"; |
||||
|
private static final String APP_SECRET = "e4Ugj6LytHX52AXizuNvanwZkVqXAB4J"; |
||||
|
|
||||
|
private static final String BASE_URL = "http://localhost:8080"; |
||||
|
|
||||
|
public static void main(String[] args) throws Exception { |
||||
|
System.out.println("========================================"); |
||||
|
System.out.println(" 模拟第三方请求 - 完整加签示例"); |
||||
|
System.out.println("========================================\n"); |
||||
|
|
||||
|
// ① 生成签名参数
|
||||
|
String timestamp = String.valueOf(System.currentTimeMillis()); |
||||
|
String nonce = IdUtil.fastSimpleUUID(); |
||||
|
|
||||
|
Map<String, String> signParams = new HashMap<>(); |
||||
|
signParams.put("appKey", APP_KEY); |
||||
|
signParams.put("timestamp", timestamp); |
||||
|
signParams.put("nonce", nonce); |
||||
|
|
||||
|
String signContent = ApiSignUtil.buildSignContent(signParams); |
||||
|
String sign = ApiSignUtil.hmacSign(signContent, APP_SECRET); |
||||
|
|
||||
|
System.out.println("【签名参数】"); |
||||
|
System.out.println(" App-Key: " + APP_KEY); |
||||
|
System.out.println(" Timestamp: " + timestamp); |
||||
|
System.out.println(" Nonce: " + nonce); |
||||
|
System.out.println(" 签名原文: " + signContent); |
||||
|
System.out.println(" Sign: " + sign); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// ② 构建请求体
|
||||
|
JSONObject body = new JSONObject(); |
||||
|
body.put("planType", 1); |
||||
|
body.put("planDate", "2026-09-03"); |
||||
|
body.put("planTime", 1); |
||||
|
body.put("enforceContent", "日常环保执法检查0904"); |
||||
|
body.put("enterpriseName", "深新粤云(深圳)信息技术有限公司圳市XX科技有限公司"); |
||||
|
body.put("enterpriseNumber", "91440300769156389U"); |
||||
|
body.put("contactPerson", "张三"); |
||||
|
body.put("contactPhone", "13800138000"); |
||||
|
body.put("enforceUserNames", "测试1,测试2"); |
||||
|
body.put("enforceUserIds", "17608417058,17608416858"); |
||||
|
body.put("deptId", "20210922212955057-A601-27251E588"); |
||||
|
body.put("deptName", "深圳市司法局"); |
||||
|
body.put("createBy", "17608417058"); |
||||
|
body.put("remark", "测试数据"); |
||||
|
body.put("outSerialNo", "LG20260903001"); |
||||
|
|
||||
|
System.out.println("【请求体】"); |
||||
|
System.out.println(" " + body.toJSONString()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// ③ 发起 HTTP 请求
|
||||
|
String url = BASE_URL + "/external/api/lg/plan/saveOrUpdatePlan"; |
||||
|
System.out.println("【发送请求】"); |
||||
|
System.out.println(" URL: " + url); |
||||
|
|
||||
|
HttpResponse response = HttpRequest.post(url) |
||||
|
.header("App-Key", APP_KEY) |
||||
|
.header("Timestamp", timestamp) |
||||
|
.header("Nonce", nonce) |
||||
|
.header("Sign", sign) |
||||
|
.header("Content-Type", "application/json;charset=UTF-8") |
||||
|
.body(body.toJSONString()) |
||||
|
.timeout(10000) |
||||
|
.execute(); |
||||
|
|
||||
|
System.out.println(" 状态码: " + response.getStatus()); |
||||
|
System.out.println(); |
||||
|
|
||||
|
// ④ 输出响应
|
||||
|
String responseBody = response.body(); |
||||
|
System.out.println("【响应结果】"); |
||||
|
try { |
||||
|
JSONObject json = JSONObject.parseObject(responseBody); |
||||
|
System.out.println(" " + json.toString()); |
||||
|
} catch (Exception e) { |
||||
|
System.out.println(" " + responseBody); |
||||
|
} |
||||
|
|
||||
|
response.close(); |
||||
|
System.out.println(); |
||||
|
System.out.println("========================================"); |
||||
|
System.out.println(" 请求完成"); |
||||
|
System.out.println("========================================"); |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue