0

Just trying to send a POST request with this simple code

  URL urlObj = new URL("http://localhost/myApp");
  HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();

  con.setRequestMethod("POST");
  con.setRequestProperty("Content-Type", "application/json");
  con.setDoOutput(true);

  DataOutputStream dataOutputStream = new DataOutputStream(con.getOutputStream());
  dataOutputStream.writeBytes("test data");
  dataOutputStream.flush();
  dataOutputStream.close();

The request above is not being sent UNTIL I add the additional lines

int responseCode = con.getResponseCode();
System.out.println("Response Code : " + responseCode);

Am I missing something? why it's not sending?

Tried to breakpoint on the server taking the request but nothing is reaching it at all

Basemm
  • 1,204
  • 14
  • 16
  • Possible duplicate of [Can you explain the HttpURLConnection connection process?](http://stackoverflow.com/questions/10116961/can-you-explain-the-httpurlconnection-connection-process) – slim Feb 16 '17 at 11:54
  • The basic answer is that HTTPUrlConnection is weird, which is why sane Java developers use alternatives such as Apache HttpComponents (except maybe in environments such as Android, where size matters). See the linked dupe for gory details. – slim Feb 16 '17 at 11:57
  • Or http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection – slim Feb 16 '17 at 11:59

0 Answers0