Click to See Complete Forum and Search --> : What POST values do I use for this form?


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>

Hughbedo
08-29-2006, 07:56 AM
It has been suggested that this message is not clear. I am sorry if that is the case.

What I am trying to do is submit data as a web form to a URL using the POST method (as opposed to the GET method). I have managed to do this to a simple example CGI script [1] using the java code in the above post.

I am trying to get the same code working with my form. I have put my development machine on the web, and you can see the form at 81.187.36.8:18080/smsserver/faces/test2.jsp (I hope). You will notice that if you press the button the text changes (from "not pressed" to "pressed" and then to "no pressed") and if you select something rom the drop down box that item is put in the text.

I am trying to replicate this behaviour with my java application rather than the browser. Whatever I put in as values submitted by the POST method I do not get any behaviour from the web server, ie. the text always remains as "not pressed".

I hope this is more clear. Please let me know if there is anything you do not understand.

[1] Script at http://www.cgi101.com/book/ch4/post.cgi web form I am copying at http://www.cgi101.com/book/ch4/form.html

ray326
08-29-2006, 01:43 PM
This was what was sent when I selected the fifth item in the list:

Content-Type: application/x-www-form-urlencoded
Content-Length: 40
form1%3Adropdown1=not+a+unit&form1=form1

I recommend you get the Live HTTP headers extension for Firefox and examine the browser/server conversation yourself.

Hughbedo
08-30-2006, 09:19 AM
I got LiveHeaders, it is very usefull thank you.

I think I know what is happening. If I restart the server and then make my first request to this page a POST request (eg. send the data "form1%3Adropdown1=%2B447123456789&form1=form1") from the browser I get the same behaviour as with the java client. Does anyone knwo how to keep the same connection and send another request? I shall ask this question on a java forum.

ray326
08-30-2006, 02:35 PM
You can try to use HTTP 1.1 and send a keep-alive header with your request but in general you'll have to make another request or put everything you want to send in the one request.