Login | Register   
Twitter
RSS Feed
Download our iPhone app
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 29, 2003

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
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap