瀏覽代碼

feat:模拟开放API的客户端,模拟第三方使用开放的API

yang yi 11 月之前
父節點
當前提交
c2686e36df
共有 2 個文件被更改,包括 72 次插入0 次删除
  1. 28 0
      open-api-client/pom.xml
  2. 44 0
      open-api-client/src/main/java/space/anyi/client/ClientApplication.java

+ 28 - 0
open-api-client/pom.xml

@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>space.anyi</groupId>
+    <artifactId>open-api-client</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>17</maven.compiler.source>
+        <maven.compiler.target>17</maven.compiler.target>
+    </properties>
+
+    <dependencies>
+        <dependency>
+            <groupId>space.anyi</groupId>
+            <artifactId>open-api-commom</artifactId>
+            <version>1.0-SNAPSHOT</version>
+        </dependency>
+        <dependency>
+            <groupId>cn.hutool</groupId>
+            <artifactId>hutool-all</artifactId>
+            <version>5.8.16</version>
+        </dependency>
+    </dependencies>
+</project>

+ 44 - 0
open-api-client/src/main/java/space/anyi/client/ClientApplication.java

@@ -0,0 +1,44 @@
+package space.anyi.client;
+
+import cn.hutool.crypto.digest.MD5;
+import cn.hutool.http.Header;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpResponse;
+import cn.hutool.json.JSONUtil;
+import space.anyi.openApi.comom.model.User;
+
+import java.nio.charset.StandardCharsets;
+
+/**
+ * @ProjectName: yangyi-open-api-platform
+ * @FileName: ClientApplication
+ * @Author: 杨逸
+ * @Data:2025/10/18 14:28
+ * @Description:
+ */
+public class ClientApplication {
+    public static final String ACCESS_KEY = "accessKey";
+    public static final String SECRET_KEY = "secretKey";
+    public static void main(String[] args) {
+        String url = "http://localhost:10002/open-api/user/body";
+        User user = new User("杨逸");
+        String json = JSONUtil.toJsonStr(user);
+        String timestamp = System.currentTimeMillis() + "";
+        HttpResponse httpResponse = HttpRequest.post(url)
+                .header("accessKey",ACCESS_KEY)//accessKey
+                .header("timestamp", timestamp)//时间戳
+                .header("sign",getSign(timestamp))//摘要签名
+                .header("nonce",""+(int)Math.random()*100)//随机数
+                .header(Header.CONTENT_TYPE, "application/json")
+                .charset(StandardCharsets.UTF_8)
+                .body(json)
+                .execute();
+        int status = httpResponse.getStatus();
+        System.out.println("status = " + status);
+        String body = httpResponse.body();
+        System.out.println("body = " + body);
+    }
+    static String getSign(String timestamp){
+        return MD5.create().digestHex16(SECRET_KEY+timestamp+SECRET_KEY);
+    }
+}