FORM submission POST to a server

2001-06-28 Thread pascal GEND



Hi,

I'd like to POST data to an http server with a java 
program (in the same way a www browser does it), in order to parse the response 
and do something with it. I have an example with the GET method but not 
POST.

For instance, consider the example 
below:

form action="http://host/cgi-bin/program" 
method=post
input type="hidden" name="field1" 
value="valuefield1"

input type="text" name="field2" 
value="valuefield2"
input type="submit" name="submit" value="submit"

What is the corresponding JAVA code (I use the JDK 117B)?

Many thanks,

Pascal


Re: FORM submission POST to a server

2001-06-28 Thread Sam Newman



hmm, I think you probably want to use the URL 
connection program. There is a javaworld tip about posting from an applet - its 
more about the security considerations, but should contain the sample code you 
want. I seem to remeber the O'Reilly java network programming book also contains 
an example use of the UrlConnection - try downloading the examples for the 
book.

The javaworld tip: http://www.javaworld.com/javatips/jw-javatip41.html, 
and the earlier tip: http://www.javaworld.com/javaworld/javatips/jw-javatip34.html/

sam

  - Original Message - 
  From: 
  pascal GEND 
  To: [EMAIL PROTECTED] 
  
  Sent: Thursday, June 28, 2001 3:30 
  PM
  Subject: FORM submission POST to a 
  server
  
  Hi,
  
  I'd like to POST data to an http server with a 
  java program (in the same way a www browser does it), in order to parse the 
  response and do something with it. I have an example with the GET method but 
  not POST.
  
  For instance, consider the example 
  below:
  
  form action="http://host/cgi-bin/program" 
  method=post
  input type="hidden" name="field1" 
  value="valuefield1"
  
  input type="text" name="field2" 
  value="valuefield2"
  input type="submit" name="submit" value="submit"
  
  What is the corresponding JAVA code (I use the JDK 117B)?
  
  Many thanks,
  
  Pascal


Re: FORM submission POST to a server

2001-06-28 Thread Sam Newman



I meant URLConnection class, not 
program!

  - Original Message - 
  From: 
  Sam 
  Newman 
  To: [EMAIL PROTECTED] 
  
  Sent: Thursday, June 28, 2001 4:19 
  PM
  Subject: Re: FORM submission POST to a 
  server
  
  hmm, I think you probably want to use the URL 
  connection program. There is a javaworld tip about posting from an applet - 
  its more about the security considerations, but should contain the sample code 
  you want. I seem to remeber the O'Reilly java network programming book also 
  contains an example use of the UrlConnection - try downloading the examples 
  for the book.
  
  The javaworld tip: http://www.javaworld.com/javatips/jw-javatip41.html, 
  and the earlier tip: http://www.javaworld.com/javaworld/javatips/jw-javatip34.html/
  
  sam


Re: FORM submission POST to a server

2001-06-28 Thread pete

Be aware that the Java URLConnection class only supports HTTP 1.0, which 
causes problems when posting to some of the services we run on IIS, 
which return replies using HTTP1.1 rendering them unhandle-able with 
URLConnection.

Also be aware that a '500' HTTP error will result in a 
'FileNotFoundException' which is exceptionally unhelpful if you ever 
have to deal with SOAP services that use a code 500 to report 
'legitimate' errors as opposed to an 'Internal Server Error' ala Apache.

I have found it worthwhile writing a socket-based POST handler, as below:

This code is ugly, inefficient and may or may not suck in your opinion. 
If you have improvements, i'd appreciate it if you'd post them back to me.

Pass your POST message as a URL-Encoded string, and you'll probably need 
to set content-type as application/x-www-formencoded (you'll have to 
look up the exact encoding name)

i.e.
URLEncoder u=new URLEncoder();
String 
Message=u.encode(field1=valuefield1field2=valuefield2submit=submit);
postSOAPMessageSock(yourserver,/cgi-bin/yourcgi.cgi,80,Message,SOAPActionifRequired);

and the post method is listed below:

public String postSOAPMessageSock(String server, String strURL,int port, 
String Message, String action)
{
try
{
Socket sock = new Socket(server, port);

// Get input and output streams for the socket connection
  DataInputStream inStream = new 
DataInputStream(sock.getInputStream());
  DataOutputStream outStream = new 
DataOutputStream(sock.getOutputStream());

  outStream.writeBytes(POST +strURL+ HTTP/1.0\r\n);

// Next, send the content type (don't forget the \r\n)
  outStream.writeBytes(Content-type: text/xml\r\n);
//Optional - required when dealing with some SOAP services 
outStream.writeBytes(SOAPAction: +action+\r\n);
outStream.writeBytes(Content-length: 
+Message.length()+\r\n);

  outStream.writeBytes(\r\n);

  outStream.writeBytes(Message);
  System.out.println(\n\nMessage Posted: 
+server+:+strURL+:+port+:+action+:+Message+\n\n);

  BufferedReader sr=new BufferedReader(new 
InputStreamReader(inStream));

//read out from the pipe a line at a time
   String line;
   boolean bodyfound=false;
   StringBuffer buffer=new StringBuffer();
   while((line=sr.readLine()) != null)
   {
   if (line.equals())
   {bodyfound=true;}
   if (bodyfound)
   {buffer.append(line);}
  //System.out.println(bodyfound+:+line);
   }
//close everything up when we're done
  sr.close();
  inStream.close();
  outStream.close();
  //System.out.println(buffer.toString());
  return buffer.toString();
}
catch (Exception e)
{
//Error is formatted as xml to avoid errors if run through a parser.
String errorstring=xmlerrorCould not contact Server./error/xml;
//e.printStackTrace();
//System.out.println(errorstring);
return errorstring;
}

}

Hope that helps

-Pete

 Hi,
 
  
 
 I'd like to POST data to an http server with a java program (in the 
 same way a www browser does it), in order to parse the response and do 
 something with it. I have an example with the GET method but not POST.
 
  
 
 For instance, consider the example below:
 
  
 
 form action=http://host/cgi-bin/program; method=post
 
 input type=hidden name=field1 value=valuefield1
 
 input type=text name=field2 value=valuefield2
 
 input type=submit name=submit value=submit
 
  
 
 What is the corresponding JAVA code (I use the JDK 117B)?
 
  
 
 Many thanks,
 
  
 
 Pascal