Skip to content

Sending Java POST request without calling getInputStream()

I would like to send a POST request in Java. At the moment, I do it like this:

URL url = new URL("myurl");
URLConnection con = url.openConnection();
PrintStream ps = new PrintStream(con.getOutputStream());
ps.println("key=" + URLEncoder.encode("value"));
// we have to get the input stream in order to actually send the request

I do not understand why I have to call con.getInputStream() in order to actually send the request. If I do not call it, the request is not sent.

Is there a problem using PrintStream? It should not matter if I take a PrintStream, PrintWriter or something else, right?


I think a post of another thread answered my question. Sorry, but I found it too late. You can find it here.

PS: Unfortunately, Stackoverflow added my last answer to the question as a comment, because my answer was too short. And it is not possible to mark a comment as the correct answer… Hope this one is long enough 🙂