2017-04-18 2 views
0

Ich versuche, eine POST-Anfrage von einem Java-Server (Jetty) an einen anderen Server https://example.com/api/test mit den Parametern userId, token und status zu senden.POST-Anfrage von Java-Server (Anlegestelle) senden

In Java Ich Aufrufen der Methode unter:

sendNotification("https://example.com/api/test", "test1", "test", 200) 

und ich sehe auf den Protokollen, die es erfolgreich druckt:

Sent request to https://example.com/api/test with userId test1 and token test 

Doch in den https://example.com/api/test Protokolle erhalte ich überhaupt keine Anfrage .

Allerdings, wenn ich den Befehl unten versuchen, erhalte ich die Anfrage perfekt:

curl -H "Content-Type: application/json" -X POST -d '{"userId": "[email protected]", "token": "asdfasdf", "status": 200}' https://example.com/api/test 

Was ist falsch?

Java-Methode:

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

Sollten Sie die output nach dem Schreibvorgang beenden Senden schließen? – jr593

Antwort

1

Sie haben "os.close()" nennen, die Anfrage Körper an den Remote-Server

Verwandte Themen