Hughbedo
08-29-2006, 06:14 AM
I have a web site (developed with Java Studio Creator), and I am writing a java application to interact with it. It requires data submited by forms with the POST method.
My java application can submit data to a perl CGI script, for example http://www.cgi101.com/book/ch4/post.cgi This works fine with the code at [1]. I cannot work out what values to pass to the server to replicate the behaviour I get when use the web site with a browser. The string "not pressed" should change to the value of the dropdown box (or to pressed if you click the button).
Can anyone tell me what values to pass to the server in the data string?
Thanks in advance.
[1]
private String postRequest(String unit) {
try {
// Construct data
String data = URLEncoder.encode("submit", "UTF-8") + "=" + URLEncoder.encode("submit", "UTF-8");
data += "&" + URLEncoder.encode("form1:dropdown1", "UTF-8") + "=" + URLEncoder.encode("+44712349****", "UTF-8");
data += "&" + URLEncoder.encode("form1", "UTF-8") + "=" + URLEncoder.encode("form1", "UTF-8");
System.out.println("data = " + data);
// Send data
//This is the URL of my development machine, does not work
URL url = new URL("http://localhost:18080/smsserver/faces/test2.jsp");
//This is the URL of a test script, does work
//URL url = new URL("http://www.cgi101.com/book/ch4/post.cgi");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", "" + data.length());
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setFollowRedirects(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
[2]
data = submit=submit&form1%3Adropdown1=%2B44712349****&form1=form1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en-GB" lang="en-GB"><head><meta http-equiv="Cache-Control" content="no-cache"/><meta http-equiv="Pragma" content="no-cache"/><title>test2 Title</title><link type="text/css" rel="stylesheet" href="resources/stylesheet.css"/></head><body style="-rave-layout: grid"><form id="form1" method="post" action="/smsserver/faces/test2.jsp;jsessionid=7AA1A70CCA55174AF126E3223F1C289F" enctype="application/x-www-form-urlencoded">
<span id="form1:outputText1" style="left: 168px; top: 72px; position: absolute">not pressed</span><input id="form1:button1" type="submit" name="form1:button1" value="Submit" style="left: 336px; top: 72px; position: absolute" /><select id="form1:dropdown1" name="form1:dropdown1" size="1" onchange="this.form.submit();" style="left: 192px; top: 144px; position: absolute"> <option value=""></option>
<option value="not a unit">not a unit</option>
<option value="+44773839****">+44773839****</option>
<option value="+44776779****">+44776779****</option>
<option value="+44770439****">+44770439****</option>
<option value="+44777087****">+44777087****</option>
<option value="+44712349****">+44712349****</option>
<option value="+901">+901</option>
<option value="Direct Golf">Direct Golf</option>
</select><input type="hidden" name="form1" value="form1" /></form></body></html>
My java application can submit data to a perl CGI script, for example http://www.cgi101.com/book/ch4/post.cgi This works fine with the code at [1]. I cannot work out what values to pass to the server to replicate the behaviour I get when use the web site with a browser. The string "not pressed" should change to the value of the dropdown box (or to pressed if you click the button).
Can anyone tell me what values to pass to the server in the data string?
Thanks in advance.
[1]
private String postRequest(String unit) {
try {
// Construct data
String data = URLEncoder.encode("submit", "UTF-8") + "=" + URLEncoder.encode("submit", "UTF-8");
data += "&" + URLEncoder.encode("form1:dropdown1", "UTF-8") + "=" + URLEncoder.encode("+44712349****", "UTF-8");
data += "&" + URLEncoder.encode("form1", "UTF-8") + "=" + URLEncoder.encode("form1", "UTF-8");
System.out.println("data = " + data);
// Send data
//This is the URL of my development machine, does not work
URL url = new URL("http://localhost:18080/smsserver/faces/test2.jsp");
//This is the URL of a test script, does work
//URL url = new URL("http://www.cgi101.com/book/ch4/post.cgi");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Length", "" + data.length());
conn.setRequestProperty("Content-Language", "en-US");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setFollowRedirects(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
// Process line...
System.out.println(line);
}
wr.close();
rd.close();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
[2]
data = submit=submit&form1%3Adropdown1=%2B44712349****&form1=form1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en-GB" lang="en-GB"><head><meta http-equiv="Cache-Control" content="no-cache"/><meta http-equiv="Pragma" content="no-cache"/><title>test2 Title</title><link type="text/css" rel="stylesheet" href="resources/stylesheet.css"/></head><body style="-rave-layout: grid"><form id="form1" method="post" action="/smsserver/faces/test2.jsp;jsessionid=7AA1A70CCA55174AF126E3223F1C289F" enctype="application/x-www-form-urlencoded">
<span id="form1:outputText1" style="left: 168px; top: 72px; position: absolute">not pressed</span><input id="form1:button1" type="submit" name="form1:button1" value="Submit" style="left: 336px; top: 72px; position: absolute" /><select id="form1:dropdown1" name="form1:dropdown1" size="1" onchange="this.form.submit();" style="left: 192px; top: 144px; position: absolute"> <option value=""></option>
<option value="not a unit">not a unit</option>
<option value="+44773839****">+44773839****</option>
<option value="+44776779****">+44776779****</option>
<option value="+44770439****">+44770439****</option>
<option value="+44777087****">+44777087****</option>
<option value="+44712349****">+44712349****</option>
<option value="+901">+901</option>
<option value="Direct Golf">Direct Golf</option>
</select><input type="hidden" name="form1" value="form1" /></form></body></html>