编辑代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class ApiCaller {
    public static void main(String[] args) {
        try {
            // 接口地址
            String apiUrl = "https://cdsp.ppsu.edu.cn/cdsp/data-api/v2/DS0001";
            // 假设已经生成的签名等鉴权信息(实际需要根据文档规则生成)
            String appId = "yourAppId";
            String timestamp = "yourTimestamp";
            String signature = "yourSignature";

            // 构建完整的请求URL,带上鉴权信息(这里只是简单示例,实际可能需要根据API要求的格式添加)
            String fullUrl = apiUrl + "?appId=" + appId + "&timestamp=" + timestamp + "&signature=" + signature;

            URL url = new URL(fullUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET"); // 根据文档,支持POST和GET,这里示例使用GET

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine())!= null) {
                    response.append(inputLine);
                }
                in.close();
                System.out.println(response.toString());
            } else {
                System.out.println("请求失败,错误码: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}