啊鑫
10 天以前 a92e3aeb50efc6ef252d0fbaa2d45b4b7a3f5c61
src/main/java/com/gs/xiaomi/service/BCS101ApiService.java
@@ -271,4 +271,144 @@
            throw new IOException("Empty response body");
        }
    }
    /**
     * X5协议标准实现版本
     * 基于注释要求:X5协议中使用的(国密算法)
     * APP ID:bcs_fty_177301, APP Key:IJBVGMJXG4ZTAMLSMFXGI33NONQWY5DG
     */
    public String getBCS101DataX5Standard(BCS101Request request) throws IOException {
        System.out.println("=== BCS101 API X5协议标准实现 ===");
        // 1. 构建JSON数据
        String jsonData = objectMapper.writeValueAsString(request);
        System.out.println("1. 原始JSON: " + jsonData);
        // 2. Base64编码
        String base64Data = X5StringUtils.encodeBase64(jsonData);
        System.out.println("2. Base64编码: " + base64Data);
        // 3. URL编码
        String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8");
        System.out.println("3. URL编码: " + urlEncodedData);
        // 4. 构建form数据
        String formData = "data=" + urlEncodedData;
        System.out.println("4. Form数据: " + formData);
        // 5. X5协议参数
        String appId = DataAcquisitionConfiguration.BCS_APP_ID;
        String appKey = DataAcquisitionConfiguration.BCS_APP_Key;
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        String nonce = String.valueOf(System.currentTimeMillis()); // 使用时间戳作为nonce
        // 6. X5协议签名 - 标准算法: hash(appId + timestamp + nonce + appKey + data)
        String signString = appId + timestamp + nonce + appKey + formData;
        String signature;
        try {
            signature = SmUtil.sm3(signString);
            System.out.println("5. 使用SM3签名");
        } catch (Exception e) {
            signature = DigestUtil.md5Hex(signString).toUpperCase();
            System.out.println("5. 使用MD5签名 (SM3不可用)");
        }
        System.out.println("6. 签名字符串: " + signString);
        System.out.println("7. 签名值: " + signature);
        // 7. Basic Auth
        String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD;
        String auth = X5StringUtils.encodeBase64(credentials);
        // 8. 构建请求体
        RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded"));
        // 9. 构建请求 - 添加X5协议标准头部
        Request httpRequest = new Request.Builder()
                .url(DataAcquisitionConfiguration.BCS_101_URL)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Basic " + auth)
                .addHeader("X-App-Id", appId)
                .addHeader("X-Timestamp", timestamp)
                .addHeader("X-Nonce", nonce)
                .addHeader("X-Signature", signature)
                .build();
        System.out.println("8. 发送X5标准请求...");
        // 10. 执行请求
        try (Response response = client.newCall(httpRequest).execute()) {
            System.out.println("9. 响应状态: " + response.code());
            System.out.println("10. 响应头: " + response.headers());
            if (response.body() != null) {
                String responseBody = response.body().string();
                System.out.println("11. 响应内容: " + responseBody);
                return responseBody;
            }
            throw new IOException("Empty response body");
        }
    }
    /**
     * X5协议变体2 - 不同的签名算法
     * 签名算法: hash(appKey + data + timestamp + appId)
     */
    public String getBCS101DataX5Variant(BCS101Request request) throws IOException {
        System.out.println("=== BCS101 API X5协议变体2 ===");
        // 1. 构建数据
        String jsonData = objectMapper.writeValueAsString(request);
        String base64Data = X5StringUtils.encodeBase64(jsonData);
        String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8");
        String formData = "data=" + urlEncodedData;
        // 2. X5协议参数
        String appId = DataAcquisitionConfiguration.BCS_APP_ID;
        String appKey = DataAcquisitionConfiguration.BCS_APP_Key;
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
        // 3. 变体签名算法: appKey + data + timestamp + appId
        String signString = appKey + formData + timestamp + appId;
        String signature;
        try {
            signature = SmUtil.sm3(signString);
            System.out.println("使用SM3签名变体");
        } catch (Exception e) {
            signature = DigestUtil.md5Hex(signString).toUpperCase();
            System.out.println("使用MD5签名变体");
        }
        System.out.println("签名字符串(变体): " + signString);
        System.out.println("签名值(变体): " + signature);
        // 4. Basic Auth
        String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD;
        String auth = X5StringUtils.encodeBase64(credentials);
        // 5. 构建请求 - 使用常见的X5头部名称
        RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded"));
        Request httpRequest = new Request.Builder()
                .url(DataAcquisitionConfiguration.BCS_101_URL)
                .method("POST", body)
                .addHeader("Content-Type", "application/x-www-form-urlencoded")
                .addHeader("Authorization", "Basic " + auth)
                .addHeader("appId", appId)
                .addHeader("timestamp", timestamp)
                .addHeader("signature", signature)
                .build();
        System.out.println("发送X5变体请求...");
        try (Response response = client.newCall(httpRequest).execute()) {
            System.out.println("变体响应状态: " + response.code());
            if (response.body() != null) {
                String responseBody = response.body().string();
                System.out.println("变体响应内容: " + responseBody);
                return responseBody;
            }
            throw new IOException("Empty response body");
        }
    }
}