Java:Can';t在Catch中发送HTTP Post请求
我需要将请求发布到catch内部的API以存储一些日志。 但当我把请求放在catch中时,它返回:Java:Can';t在Catch中发送HTTP Post请求,java,http,try-catch,com.sun.net.httpserver,Java,Http,Try Catch,Com.sun.net.httpserver,我需要将请求发布到catch内部的API以存储一些日志。 但当我把请求放在catch中时,它返回: java.io.IOException:服务器返回URL的HTTP响应代码:500 代码: try { ... } catch (Exception e) { postRequest(...); } 对API的代码Post请求 public static Object postRequest(...) throws IOException, ParseException {
java.io.IOException:服务器返回URL的HTTP响应代码:500
代码:
try {
...
} catch (Exception e) {
postRequest(...);
}
对API的代码Post请求
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;
}
}
siml问题出在服务器端,因为您的请求5xx是服务器错误。@VinayPrajapati问题出在服务器端,因为当我在catch之外调用postRequest()函数时,它工作正常。