2017-04-18 18 views
0

Javaサーバー(Jetty)から別のサーバーにuserIdtokenおよびstatusのPOST要求を送信しようとしています。Javaサーバー(jetty)からPOST要求を送信

私は以下のメソッドを呼び出すのですJavaでは

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); 
     } 
    } 
+1

にリクエストボディを送信終了する「にos.closeを()」を呼び出す必要がありますか? – jr593

答えて

1

あなたが書き込み操作の後にOutputStreamを閉じる必要がありますあなたは、リモートサーバー

関連する問題