0

我需要将请求发布到 catch 内的 API 以存储一些日志。但是当我把它放在catch里面的时候,它返回了: java.io.IOException: Server returned HTTP response code: 500 for URL

代码:

try {
    ...
} catch (Exception e) {
    postRequest(...);
}

对 API 的代码发布请求;

public static Object postRequest(...) throws IOException, ParseException {

    URL url = new URL(API + "/" + pathName);
    HttpURLConnection connection = getHttpURLConnection(url);

    try (OutputStream os = connection.getOutputStream()) {
        byte[] input = body.getBytes("utf-8");
        os.write(input, 0, input.length);
    }

    try {
        StringBuilder response = new StringBuilder();
        InputStreamReader inputStreamReader = new InputStreamReader(connection.getInputStream(), "utf-8");
        BufferedReader br = new BufferedReader(inputStreamReader);
        String responseLine = null;
        while ((responseLine = br.readLine()) != null) {
            response.append(responseLine.trim());
        }


        JSONParser parser = new JSONParser();
        JSONObject obj = (JSONObject) parser.parse(response.toString());

        return obj;

    } catch (IOException err) {
        return null;
    }
}
4

0 回答 0