The moose likes Sockets and Internet Protocols and the fly likes Automate POST in URLConnection Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Reply Bookmark "Automate POST in URLConnection" Watch "Automate POST in URLConnection" New topic
Author

Automate POST in URLConnection

q yuan
Greenhorn

Joined: Nov 16, 2001
Posts: 8
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

Joined: Jan 06, 2002
Posts: 111
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.


" Veni, vidi, vici "<br />" I came, I saw, I conquered "
 
 
subject: Automate POST in URLConnection
 
Threads others viewed
Saving HTML file
yahoo mail, gmail setup in my web application
Servlet flow
Session problem or the browser problem?
Redirecting servlet response between windows
IntelliJ Java IDE