0
Javaサーバー(Jetty)から別のサーバーにuserId
、token
およびstatus
のPOST要求を送信しようとしています。Javaサーバー(jetty)からPOST要求を送信
:
sendNotification("https://example.com/api/test", "test1", "test", 200)
を、私はそれが正常に出力したログに次を参照してください。
Sent request to https://example.com/api/test with userId test1 and token test
をしかし、私はまったく要求を受信しないhttps://example.com/api/test
ログに。
私は、以下のコマンドを実行しようとすると、しかし、私は完全に要求を受信します:
curl -H "Content-Type: application/json" -X POST -d '{"userId": "[email protected]", "token": "asdfasdf", "status": 200}' https://example.com/api/test
間違っていますか?
Javaメソッド:
private void sendNotification(String endpoint, String userId,
String token, int status) throws IOException {
URL url = new URL(endpoint);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection)con;
http.setRequestMethod("POST");
http.setDoOutput(true);
byte[] out = ("{\"userId\":\"" + userId + "\",\"token\":\"" + token + "\",\"status\":" + status + "}").getBytes(StandardCharsets.UTF_8);
int length = out.length;
http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
http.connect();
try {
log.warn("Sent request to " + endpoint + " with userId " + userId + " and token " + token);
OutputStream os = http.getOutputStream();
os.write(out);
}
catch(IOException e) {
log.error(APImessages.ERROR_500);
}
}
にリクエストボディを送信終了する「にos.closeを()」を呼び出す必要がありますか? – jr593