Skip to content
Advertisement

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();
con.setDoOutput(true);
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
con.getInputStream();  
ps.close();

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?

Advertisement

Answer

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 🙂

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement