import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; public class WapiClient { private final String method; // "GET" or "POST" private final String secretType; // "MD5" or "Hash" private final String apiUrl; private final String appId; private final String secret; public WapiClient(String method, String secretType, String apiUrl, String appId, String secret) { this.method = method; this.secretType = secretType; this.apiUrl = apiUrl; this.appId = appId; this.secret = secret; } public String makeRequest(Map params) throws Exception { // Add required parameters Map data = new HashMap<>(params); data.put("appid", appId); data.put("time", String.valueOf(System.currentTimeMillis() / 1000)); // Generate signature String sign; if ("MD5".equalsIgnoreCase(secretType)) { sign = generateMd5Signature(data); } else { // Default to MD5 if secretType is not recognized sign = this.secret; } data.put("sign", sign); if ("GET".equalsIgnoreCase(method)) { return sendGetRequest(data); } else { return sendPostRequest(data); } } private String generateMd5Signature(Map data) throws Exception { // Sort keys alphabetically List keys = new ArrayList<>(data.keySet()); Collections.sort(keys); // Build the string to hash StringBuilder sb = new StringBuilder(); for (String key : keys) { String value = data.get(key); if (value != null && !value.isEmpty()) { sb.append(key).append(value); } } sb.append(secret); // Calculate MD5 hash MessageDigest md = MessageDigest.getInstance("MD5"); byte[] hashBytes = md.digest(sb.toString().getBytes(StandardCharsets.UTF_8)); // Convert byte array to hex string StringBuilder hexString = new StringBuilder(); for (byte b : hashBytes) { String hex = Integer.toHexString(0xff & b); if (hex.length() == 1) { hexString.append('0'); } hexString.append(hex); } return hexString.toString(); } private String sendGetRequest(Map data) throws Exception { // Build query string StringBuilder queryString = new StringBuilder(); for (Map.Entry entry : data.entrySet()) { if (queryString.length() > 0) { queryString.append("&"); } queryString.append(URLEncoder.encode(entry.getKey(), "UTF-8")) .append("=") .append(URLEncoder.encode(entry.getValue(), "UTF-8")); } URL url = new URL(apiUrl + "?" + queryString); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); // Read response try (BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } return response.toString(); } } private String sendPostRequest(Map data) throws Exception { // Build form data StringBuilder formData = new StringBuilder(); for (Map.Entry entry : data.entrySet()) { if (formData.length() > 0) { formData.append("&"); } formData.append(URLEncoder.encode(entry.getKey(), "UTF-8")) .append("=") .append(URLEncoder.encode(entry.getValue(), "UTF-8")); } URL url = new URL(apiUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); conn.setDoOutput(true); // Write form data conn.getOutputStream().write(formData.toString().getBytes(StandardCharsets.UTF_8)); // Read response try (BufferedReader reader = new BufferedReader( new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8))) { StringBuilder response = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { response.append(line); } return response.toString(); } } public static void main(String[] args) { // Configuration - replace with your actual values String method = "GET"; // "GET" or "POST" String secretType = "MD5"; // 验证方式MD5,Hash 通过后台我的应用(https://www.wapi.cn/member/my_apply.html)去修改 String apiUrl = "https://登录后显示/api/22/128"; String appId = "应用id"; // 通过后台我的应用(https://www.wapi.cn/member/my_apply.html) String secret = "应用密钥"; // Replace with your secret key // Request parameters Map params = new HashMap<>(); params.put("appid", "1"); params.put("date", "2021-08-17"); params.put("format", "json"); params.put("time", "1545829466") WapiClient client = new WapiClient(method, secretType, apiUrl, appId, secret); try { String result = client.makeRequest(params); System.out.println("API Response:"); System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }