dcsimg
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
Dec 30, 2003

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


Posting Forms Programmatically

Posting forms in Java is easy. All you need is just a HTML code snippet. Here's an example:
 
<form action="getmsg.asp?q=1" method="post">
Your Email : *                                  
<input type=text name=strEmail maxlength=255>
Message : *                                          
<input type=text name=strMessage maxlength=70>
<input name="submit" type="submit" value="Send>
</form>
Post the above form like this:

String server ="http://www.server.com/getmsg.asp?q=1";

 

HttpURLConnection uc =(HttpURLConnection) new

                                  URL(server).openConnection();

uc.setDoOutput(true);

uc.setRequestMethod("POST");

 

 

BufferedWriter bw = new BufferedWriter(new  

                       OutputStreamWriter(uc.getOutputStream()));

 

String query="";


query += URLEncoder.encode("strEmail")+ "="+ 

                   URLEncoder.encode("silver@hotmail.com");

query +="&";

query += URLEncoder.encode("strMessage")+ "=" +

  URLEncoder.encode("Prices are going down!");

 

 

     bw.write(query);

     bw.write("\r\n");

     bw.flush();

     bw.close();
Though this code posts only two values, this method can be used to post any number of values.
M.Fahim Zahid
 
Thanks for your registration, follow us on our social networks to keep up-to-date