I'm trying to post data on a server using java HttpUrlConnection class. It seems that if I somehow read the response of the server, the post works fine, but if I don't, the information is never posted.
This kind of behaviour is not mentionned in the HttpUrlConnection doc and all examples I've seen of HttUrlConnection ask for a response from the server.
I want to know if I made a mistake in my code or if this is a normal behaviour of HttpUrlConnection, and if it's the case, can someone with a better understanding of how this class works explain to me why it's so?
Thanks a lot!
Here is the code, the commented line is the one that makes the POST either work or fail:
URL url=new URL("myUrl");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
connection.setRequestProperty("Host", "myHost");
connection.setDoOutput(true);
os.write(staffToPost.getBytes("utf-8"));
os.flush();
os.close();
//System.out.println(connection.getResponseCode()+" "+connection.getResponseMessage());