This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Automate POST in URLConnection  RSS feed

 
q yuan
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to read a POST result from a certain URL. If the request page (where you will fill out a form and click submit) and the response page (where the result is shown) are the same URL, it is easy. You write the post parameters to the URL and then open an inputstream to read the result. However, how does it work if the request page and the response page are 2 different URL? Since the request page is using POST as the form method, you can't just open the response page and pass paramters after the URL. Anyone has any ideas? Thanks in advance,
 
Laudney Ren
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do it this way:
URl url = new URL("http://www.laudney.org/cgi-bin/post-query");
URLConnection uc = url.openConnection();
uc.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(uc.getOutputStream(),"ASCii");
out.write(query.toString());
out.write("\r\n");
out.flush();
out.close();
InputStream in = uc.getInputStream();
// read the response from in
note:
setDoOutput(true) is necessary.
out.flush is necessary or the query won't be sent.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!