首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Java 整合小i機器人 API:簽名驗證與 HTTP 請求完整實作

     編輯:Java 相關知識

從簽名算法到 HttpClient 呼叫:聊天機器人接入指南

小i機器人(Xiaoi)提供 NLP 開放平台,採用 Digest Access Authentication 風格的簽名機制:AppKey + Realm + AppSecret 經 SHA-1 產生 HA1,Method + URI 產生 HA2,再結合 nonce 產生最終 signature。本範例封裝完整流程。

核心簽名邏輯

String realm = "xiaoi.com";
String method = "POST";
String uri = "/robot/ask.do";

// 1. 隨機 nonce(20 bytes → 40 hex chars)
byte[] b = new byte[20];
new Random().nextBytes(b);
String nonce = new String(Hex.encodeHex(b));

// 2. HA1 = SHA1(AppKey:realm:AppSecret)
String HA1 = DigestUtils.shaHex(AppKey + ":" + realm + ":" + AppSecret);

// 3. HA2 = SHA1(Method:URI)
String HA2 = DigestUtils.shaHex(method + ":" + uri);

// 4. signature = SHA1(HA1:nonce:HA2)
String signature = DigestUtils.shaHex(HA1 + ":" + nonce + ":" + HA2);

// 5. 請求頭
String authHeader = "app_key=\"" + AppKey + "\", nonce=\"" + nonce + "\", signature=\"" + signature + "\"";

HttpClient 呼叫封裝

HttpClient client = new HttpClient();
PostMethod post = new PostMethod("https://nlp.xiaoi.com/robot/ask.do");
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
post.addRequestHeader("X-Auth", authHeader);
post.setParameter("platform", "weixin");
post.setParameter("type", "0");
post.setParameter("userId", userId);    // 使用者識別
post.setParameter("question", question); // 使用者提問

int status = client.executeMethod(post);
if (status == 200) return post.getResponseBodyAsString();

依賴庫(Maven)

<dependency>
  <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version>
</dependency>
<dependency>
  <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version>
</dependency>
<dependency>
  <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version>
</dependency>

注意:commons-httpclient 為舊版(3.x),新專案建議改用 HttpComponents HttpClient 4.5+ 或 OkHttp,簽名邏輯不變,只需調整請求建構方式。

安全與維運建議

  • 密鑰管理:APP_KEY/SECRET 勿硬編碼,放入配置中心或環境變數。
  • 重試與熔斷:網路抖動時指數退避重試 3 次,引入 Resilience4j/Sentinel 熔斷。
  • 日誌脫敏:記錄請求時隱藏 signature、question 等敏感資訊。
  • HTTPS:生產環境強制使用 https://nlp.xiaoi.com,驗證伺服器憑證。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved