| | |
| | | import com.fasterxml.jackson.databind.ObjectMapper; |
| | | import com.gs.xiaomi.config.DataAcquisitionConfiguration; |
| | | import com.gs.xiaomi.dto.BCS101Request; |
| | | import com.gs.xiaomi.dto.X5Header; |
| | | import com.gs.xiaomi.dto.X5Request; |
| | | import com.gs.xiaomi.dto.X5Response; |
| | | import com.gs.xiaomi.util.X5StringUtils; |
| | | import okhttp3.*; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.IOException; |
| | | import java.net.URLEncoder; |
| | | import java.nio.charset.StandardCharsets; |
| | | import java.util.concurrent.TimeUnit; |
| | | |
| | | @Service |
| | |
| | | } |
| | | |
| | | /** |
| | | * 获取BCS101数据 |
| | | * 获取BCS101数据 - 严格按照C# X5协议实现 |
| | | * <p> |
| | | * 基于C# Helper102.POSTResponseData方法的Java实现 |
| | | * X5协议流程: |
| | | * 1. 序列化Body为JSON字符串 |
| | | * 2. MD5签名: MD5(appid+bodyStr+appkey) |
| | | * 3. 构建X5Request {header: {appid, sign}, body: bodyStr} |
| | | * 4. Base64编码整个X5Request |
| | | * 5. URL编码 |
| | | * 6. 发送 data=url编码(base64编码)的数据 |
| | | * |
| | | * @param request BCS101请求参数 |
| | | * @return API响应 |
| | | * @throws IOException 网络异常 |
| | | * 接口返回 |
| | | * Error: ERROR_IN_MODULECHAIN;ERROR_IN_MODULECHAIN, Sender Channel 'CC_MI_OEM_HTTP_OUT' (ID: a58369adbaa03aafb76d2c02c2ae7cd7): Catching exception calling messaging system |
| | | * 我是需要X5协议的, 我看你没有为我添加 |
| | | * APP ID:bcs_fty_177301 |
| | | * APP Key:IJBVGMJXG4ZTAMLSMFXGI33NONQWY5DG |
| | | */ |
| | | public String getBCS101Data(BCS101Request request) throws IOException { |
| | | // 直接将请求对象转换为JSON字符串 |
| | | String jsonData = objectMapper.writeValueAsString(request); |
| | | try { |
| | | System.out.println("=== BCS101 X5协议实现 (基于C#代码) ==="); |
| | | |
| | | // Base64编码 |
| | | String base64Data = X5StringUtils.encodeBase64(jsonData); |
| | | // 1. 序列化Body为JSON字符串 |
| | | String bodyStr = objectMapper.writeValueAsString(request); |
| | | System.out.println("1. Body JSON: " + bodyStr); |
| | | |
| | | // URL编码 |
| | | String urlEncodedData = URLEncoder.encode(base64Data, String.valueOf(StandardCharsets.UTF_8)); |
| | | // 2. MD5签名: sign = MD5(appid + bodyStr + appkey) |
| | | String appId = DataAcquisitionConfiguration.BCS_APP_ID; |
| | | String appKey = DataAcquisitionConfiguration.BCS_APP_Key; |
| | | String signString = appId + bodyStr + appKey; |
| | | String sign = DigestUtil.md5Hex(signString).toUpperCase(); |
| | | System.out.println("2. 签名字符串: " + signString); |
| | | System.out.println("3. MD5签名: " + sign); |
| | | |
| | | // 构建form表单数据 |
| | | String formData = "data=" + urlEncodedData; |
| | | // 3. 构建X5Header |
| | | X5Header header = new X5Header(appId, sign); |
| | | |
| | | // 生成时间戳 |
| | | String timestamp = String.valueOf(System.currentTimeMillis() / 1000); |
| | | // 4. 构建X5Request |
| | | X5Request x5Request = new X5Request(header, bodyStr); |
| | | String x5RequestJson = objectMapper.writeValueAsString(x5Request); |
| | | System.out.println("4. X5Request JSON: " + x5RequestJson); |
| | | |
| | | // 生成X5签名 |
| | | String signature = generateX5Signature( |
| | | DataAcquisitionConfiguration.BCS_APP_ID, |
| | | DataAcquisitionConfiguration.BCS_APP_Key, |
| | | timestamp, |
| | | formData |
| | | ); |
| | | // 5. Base64编码整个X5Request |
| | | String base64Data = X5StringUtils.encodeBase64(x5RequestJson); |
| | | System.out.println("5. Base64编码: " + base64Data); |
| | | |
| | | // 构建Authorization header |
| | | String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; |
| | | String auth = X5StringUtils.encodeBase64(credentials); |
| | | // 6. URL编码 |
| | | String urlEncodedData = URLEncoder.encode(base64Data, "UTF-8"); |
| | | System.out.println("6. URL编码: " + urlEncodedData); |
| | | |
| | | // 构建请求体 |
| | | RequestBody body = RequestBody.create(formData, MediaType.parse("application/x-www-form-urlencoded")); |
| | | // 7. 构建form表单数据: data=url编码(base64编码)的数据 |
| | | String formData = "data=" + urlEncodedData; |
| | | System.out.println("7. Form数据: " + formData); |
| | | |
| | | // 构建请求 |
| | | 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", DataAcquisitionConfiguration.BCS_APP_ID) |
| | | .addHeader("X-Timestamp", timestamp) |
| | | .addHeader("X-Signature", signature) |
| | | .build(); |
| | | // 8. Basic Auth认证 |
| | | String credentials = DataAcquisitionConfiguration.BCS_USER_NAME + ":" + DataAcquisitionConfiguration.BCS_PWD; |
| | | String auth = X5StringUtils.encodeBase64(credentials); |
| | | System.out.println("8. Basic Auth: Basic " + auth); |
| | | |
| | | // 执行请求 |
| | | try (Response response = client.newCall(httpRequest).execute()) { |
| | | if (response.body() != null) { |
| | | return response.body().string(); |
| | | // 9. 构建HTTP请求 - 严格按照C#代码的请求格式 |
| | | 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("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8") |
| | | .addHeader("Cache-Control", "no-cache") |
| | | .addHeader("Pragma", "no-cache") |
| | | .build(); |
| | | |
| | | System.out.println("9. 发送X5协议请求到: " + DataAcquisitionConfiguration.BCS_101_URL); |
| | | |
| | | // 10. 执行请求 |
| | | try (Response response = client.newCall(httpRequest).execute()) { |
| | | System.out.println("10. HTTP状态码: " + response.code()); |
| | | System.out.println("11. 响应头: " + response.headers()); |
| | | |
| | | if (response.body() != null) { |
| | | String responseBody = response.body().string(); |
| | | System.out.println("12. 原始响应: " + responseBody); |
| | | |
| | | // 11. 尝试解析为X5Response格式 |
| | | try { |
| | | X5Response x5Response = objectMapper.readValue(responseBody, X5Response.class); |
| | | System.out.println("13. X5响应解析成功: " + x5Response); |
| | | |
| | | // 检查响应状态 |
| | | if (x5Response.getHeader() != null && "200".equals(x5Response.getHeader().getCode())) { |
| | | System.out.println("14. X5协议调用成功"); |
| | | } else { |
| | | System.out.println("14. X5协议调用失败: " + |
| | | (x5Response.getHeader() != null ? x5Response.getHeader().getDesc() : "未知错误")); |
| | | } |
| | | } catch (Exception e) { |
| | | System.out.println("13. 非X5格式响应,直接返回原始内容"); |
| | | } |
| | | |
| | | return responseBody; |
| | | } |
| | | |
| | | throw new IOException("Empty response body"); |
| | | } |
| | | throw new IOException("Empty response body"); |
| | | |
| | | } catch (Exception ex) { |
| | | System.err.println("BCS101 X5协议调用异常: " + ex.getMessage()); |
| | | ex.printStackTrace(); |
| | | throw new IOException("BCS101 X5协议调用失败", ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 生成X5协议签名 |
| | | * |
| | | * @param appId APP ID |
| | | * @param appKey APP Key |
| | | * @param timestamp 时间戳 |
| | | * @param data 请求数据 |
| | | * @return 签名字符串 |
| | | */ |
| | | private String generateX5Signature(String appId, String appKey, String timestamp, String data) { |
| | | // X5签名算法: MD5(appId + appKey + timestamp + data) |
| | | String signString = appId + appKey + timestamp + data; |
| | | return DigestUtil.md5Hex(signString).toUpperCase(); |
| | | } |
| | | } |