Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

send data to CGI

0 views
Skip to first unread message

Erik Kjellson

unread,
Jun 23, 2001, 2:35:55 PM6/23/01
to
I want to send data to a CGI script via the method POST.
So I have these lines in the source, but I can't get it work, I think it
must be the casting in the first line.

How can I get rid of the casting?

HttpURLConnection connection = (HttpURLConnection) email.openConnection();
// email is the URL of the CGI script
connection.setRequestMethod("POST");
connection.setDoOutput(true);
PrintStream out = new PrintStream(connection.getOutputStream());
out.println("Name=datorn");
out.close();
} catch(MalformedURLException mue) {
showStatus("Illegal URL: " + mue);
} catch(IOException ioe) {
showStatus("IOException: " + ioe);
} catch(Exception e) {
showStatus("Error: " + e);
}

PS. I've tested URLConnection but it didn't seem to work either DS.

Best regards, Erik


Jon Skeet

unread,
Jun 23, 2001, 3:55:51 PM6/23/01
to
Erik Kjellson <erik.k...@home.se> wrote:
> I want to send data to a CGI script via the method POST.
> So I have these lines in the source, but I can't get it work, I think it
> must be the casting in the first line.

See NetUtil in my utilities package at
http://www.pobox.com/~skeet/java/skeetutil/ for an example of posting in
Java, which you can modify to do what you need to. I've always found
that the order in which you connect etc is quite fragile and badly
documented.

--
Jon Skeet - <sk...@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too

Peter Lyons

unread,
Jun 23, 2001, 5:08:48 PM6/23/01
to
you also need connection.setDoInput(true); and
connection.setAllowUserInteraction(false);

see http://www.bruececkel.com for Bruce's book Thinking in Java, which
explains how to do this in detail.

Pete

Jean Pelletier

unread,
Jun 23, 2001, 6:05:32 PM6/23/01
to
hi
1-do not use HttpURLConnection in applet because ms explorer bug
2-here is a very good post method :
tf = textfield ta = textarea
public void post1(){
try{
String data="name="+URLEncoder.encode(tf.getText());
URL u=new URL("http://localhost/cgi-bin/prog.cgi");
URLConnection uc=u.openConnection();
uc.setDoInput(true);
uc.setDoOutput(true);
uc.setUseCaches(false);

uc.setRequestProperty("Content-type","application/x-www-form-urlencoded");
uc.setRequestProperty("Content-length",""+data.length());
DataOutputStream dos=new DataOutputStream(uc.getOutputStream());
dos.writeBytes(data);
dos.close();
DataInputStream dis=new DataInputStream(uc.getInputStream());
String line;
while((line=dis.readLine()) != null){
ta.appendText(line);
ta.appendText("\n");
}
dis.close();
}
catch(Exception e){
ta.appendText("post error");
e.printStackTrace();
}
}
Erik Kjellson <erik.k...@home.se> a écrit dans le message :
ln5Z6.14146$vR1....@nntpserver.swip.net...

0 new messages