From alberta!rover.ucs.ualberta.ca!news.bc.net!news.uoregon.edu!hpg30a.csc.cuhk.hk!news.cuhk.edu.hk!agate!howland.reston.ans.net!nntp.coast.net!swidir.switch.ch!scsing.switch.ch!elna.ethz.ch!usenet Wed Jan 10 04:57:39 1996 Path: alberta!rover.ucs.ualberta.ca!news.bc.net!news.uoregon.edu!hpg30a.csc.cuhk.hk!news.cuhk.edu.hk!agate!howland.reston.ans.net!nntp.coast.net!swidir.switch.ch!scsing.switch.ch!elna.ethz.ch!usenet From: "Life is hard... and then you die." Newsgroups: comp.lang.java Subject: Re: "POST"ing to a cgi-script Date: Sat, 06 Jan 1996 15:03:19 +0100 Organization: IIS, ETHZ Lines: 195 Message-ID: <30EE8127.41C67EA6@psi.ch> References: <9601061625.AA11048@18423-news.bilbo.wipsys.soft.net> NNTP-Posting-Host: yggdrasil.ethz.ch Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="---------------------------12761865806720403171988697202" X-Mailer: Mozilla 2.0b4 (X11; I; SunOS 4.1.3 sun4m) To: trk@bilbo.wipsys.soft.net This is a multi-part message in MIME format. -----------------------------12761865806720403171988697202 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit trk@bilbo.wipsys.soft.net wrote: Hi, > I would like to create a form in java and which should > be posted to a CGI script once it is filled up. I suppose > one can take care of this situation if the cgi-script is > called by the "GET" method. But if the method is "POST", > where the script reads form contents from its stdin, how > should I go about doing it? The only difference with using POST really is that you need to specify a Content-length and Content-type and write the data following the request header. > Any help would be greatly appreciated.. I posted a simple example here of how to POST to a cgi-script a few days ago, but because of the traffic you might not have it stored on your news-server anymore. So here it is again (with a small change, and a few notes in the comments added). Cheers, Ronald -----------------------------12761865806720403171988697202 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="PostExample.java" /* * This applet is a simple example of how contact a cgi-script using the * POST method. It sends the data in sdata puts any received data into * rdata; rdata is then displayed in the applet. * * The variable script is the cgi-script to be contacted. Note: since we're * doing raw HTTP here the variable script must contain the full (absolute) * path to the cgi-script. * * Note also: The cgi-script must reside on the same machine as where the * applet was loaded from (this is a security constraint in Netscape * browsers, and will probably be around in other browsers too). */ import java.applet.Applet; import java.awt.Graphics; import java.net.Socket; import java.io.*; import java.util.StringTokenizer; public class PostExample extends Applet { private final String script = "/cgi-bin/show_input.cgi"; private final String ctype = "application/octet-stream"; private String sdata = "Hello World"; private String rdata = ""; private String home; private int port; public void init() { home = getCodeBase().getHost(); port = getCodeBase().getPort(); if (port == -1) port = 80; } public void start() { Socket sock; OutputStream outp; InputStream inp; DataOutputStream dataout; DataInputStream datain; rdata = ""; //create a client socket try sock = new Socket(home, port); catch (Exception e) { rdata = e+" (socket: Host: "+home+"\tPort: "+port+")"; return; } // Obtain output stream to communicate with the server try { outp = sock.getOutputStream(); inp = sock.getInputStream(); } catch (Exception e) { rdata = e+" (get IOStream)"; try sock.close(); catch (IOException ee) ; return; } try { dataout = new DataOutputStream(outp); datain = new DataInputStream(inp); } catch (Exception e) { rdata = e+" (get DataIOStream)"; try sock.close(); catch (IOException ee) ; return; } // Send http request to server and get return data try { // HTTP header dataout.writeBytes("POST " + script + " HTTP/1.0\r\n"); dataout.writeBytes("Content-type: " + ctype + "\r\n"); dataout.writeBytes("Content-length: " + sdata.length() + "\r\n"); dataout.writeBytes("\r\n"); // end of header // POST data dataout.writeBytes(sdata); boolean body = false; String line; while ((line = datain.readLine()) != null) { if (body) rdata += "\n" + line; else if (line.equals("")) // end of header body = true; } } catch (Exception e) { rdata = e+" (read/write data)"; try sock.close(); catch (IOException ee) ; return; } // close up shop try { dataout.close(); datain.close(); } catch (IOException e) ; try sock.close(); catch (IOException e) ; } public void stop() {} public void paint(Graphics g) { StringTokenizer st = new StringTokenizer(rdata, "\n"); int line = 1, line_sp = getFontMetrics(getFont()).getHeight()+1; while (st.hasMoreTokens()) { g.drawString(st.nextToken(), 5, line*line_sp); line++; } } } -----------------------------12761865806720403171988697202--