Post from Java Applet

C

Chris Malumphy

I need to post a string from a java applet to my server. Can this be
done? I have been fooling around with the following code. But frankly I
don't know what I'm doing and could use some help. My applet will
generate a string that I want to send to my script which will append it
to a file. Any help would be appreciated.

public boolean SendInfo(stringtosend)
{
String s1 = "POST /cgi-bin/myform.pl";
Socket http = new Socket("www.mysite.com", 80);
OutputStream os = http.getOutputStream();
try
{
PrintStream ps = new PrintStream(s1);
ps.println("POST /cgi-bin/websearch.pl");
ps.println("Content-type: plain/text");
String data = stringtosend;
ps.println("Content-length: " + data.length + "\n");
ps.println(data);
}
catch (Exception e)
return(false);
return(true);

}
 
I

Ike

Dont know if this might help you -- just some ancient code I had around here
(but I have used & debugged it and it works).....Ike

protected void doCGI_POST(String theCGI, String param[], String value[]){
/*
The POST method allows the programmer to manipulate the data
received from the CGI.
First a connection is made to the CGI, an OutputStream is open to
send the parameters (if any).
Then InputStream is created to receive the result.
*/
URL CGIurl=null;
URLConnection c=null;
DataOutputStream out=null;
try{
CGIurl = new URL(theCGI);
c = CGIurl.openConnection();
c.setDoOutput(true);
c.setUseCaches(false);

c.setRequestProperty("content-type","application/x-www-form-urlencoded");
out = new DataOutputStream(c.getOutputStream());

StringBuffer encoded = new StringBuffer(1000);
for(int x=0;x<param.length;x++){
encoded.append(param[x]);
encoded.append("=");
encoded.append(URLEncoder.encode(value[x]));
if(x<param.length-1)
encoded.append("&");
}

out.writeBytes(encoded.toString());
out.flush(); out.close();

BufferedReader in = new BufferedReader(new
InputStreamReader(c.getInputStream()));
String aLine;
while ((aLine = in.readLine()) != null) {
// data from the CGI
System.out.println(aLine);
}
}catch(Exception e){
System.out.println("Problem with CGI request! "+e);
e.printStackTrace();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top