posting to cgi from applet

I

Ike

Not sure I understand...perhaps this helps - Ike


/**
*executes a Post request to the server, for calling CGI scripts
essentially
*@param theCGI the URL pointing to the CGI script to exectue
*@param param[] an array of parameter names to pass to the script
*param value[] an array of parameter values, corresponding to the array
of parameter names
*/
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();
}
}

you call this method, for example, as follows:

String p[] ={
"recipient",
"subject",
"env_report",
"realname",
"email",
"comments"
};

String v[]= {
RFQTo,
"Request For Quote",
"HTTP_USER_AGENT,REMOTE_HOST,REMOTE_ADDR,HTTP_X_FORWARDED_FOR",
realName,
emailFrom,
"blah blah blah:\n\n" + msg
};

doCGI_POST("http://www.selectthat.com/cgi-bin/FormMail.pl", p, v);
//-Ike
 
M

Miguel De Anda

Aahhh... that looks like what I need. I'll take a good look at the
URLConnection/HttpURLConnection objects. I take it that it can do it like a
regular form post. That seems best.




Ike said:
Not sure I understand...perhaps this helps - Ike


/**
*executes a Post request to the server, for calling CGI scripts
essentially
*@param theCGI the URL pointing to the CGI script to exectue
*@param param[] an array of parameter names to pass to the script
*param value[] an array of parameter values, corresponding to the array
of parameter names
*/
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();
}
}

you call this method, for example, as follows:

String p[] ={
"recipient",
"subject",
"env_report",
"realname",
"email",
"comments"
};

String v[]= {
RFQTo,
"Request For Quote",
"HTTP_USER_AGENT,REMOTE_HOST,REMOTE_ADDR,HTTP_X_FORWARDED_FOR",
realName,
emailFrom,
"blah blah blah:\n\n" + msg
};

doCGI_POST("http://www.selectthat.com/cgi-bin/FormMail.pl", p, v);
//-Ike

Miguel De Anda said:
What is the normal/standard way to post data from an applet to the web
server? We don't want to open another port on the server and instead do it
from port 80. I was thinking that I can base64 encode the data, and put it
in a querystring like this:
http://www.mywebsite.com/myappletpost.jsp?data=lkfjlwejfowijfoweijfowei.....
Would this work well? I can't think of another way. What's the max length
that a url can have?
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top