Tuesday, September 7, 2010
 
 

Submit HTML form using Java

Java code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package post;
 
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.HttpURLConnection;
import java.net.URL;
 
public class HttpPostForm
{
  public static void main(String[] args)
  {
    try
    {
      URL url = new URL( "http://www.aaaa.com/xyz.asp" );
 
      HttpURLConnection hConnection = (HttpURLConnection)
                             url.openConnection();
      HttpURLConnection.setFollowRedirects( true );
 
      hConnection.setDoOutput( true );
      hConnection.setRequestMethod("POST");	
 
      PrintStream ps = new PrintStream( hConnection.getOutputStream() );
      ps.print("param1=abcd&param2=10341");
      ps.close();
 
      hConnection.connect();
 
      if( HttpURLConnection.HTTP_OK == hConnection.getResponseCode() )
      {
        InputStream is = hConnection.getInputStream();
        OutputStream os = new FileOutputStream("output.html");
        int data;
        while((data=is.read()) != -1)
        {
          os.write(data);
        }
        is.close();
        os.close();
        hConnection.disconnect();
      }
    }
    catch(Exception ex)
    {
      ex.printStackTrace();
    }
  }
}

Read the rest of this entry »

 

Submit HTML form using Perl or Java ?

Recently I was trying to pull data from a website. As usual, the website had a HTML page with input text boxes and after providing the necessary input, it shows up the response.

Looks simple! Right? Well, It wasn’t. Actually I wanted to pull numbers of records ( 100+ ). I realized it does not make sense to enter inputs every time and wait for the server response. I wanted to do it quick!

Looking into the page source, I found that it was indeed a simple HTML form with POST method. The basic structure of HTML form was

<form action=”xyz.asp” method=”POST”>

<input name=’param1’ value=’’/>
<input name=’param2’, value=’’/>

</form

In short, my requirement was to write a program which should take values for parameters ‘param1’ and ‘param2’ and hits the action page using POST method!
Read the rest of this entry »

 
 
 

 

Find Me!
View Kunal Kumar's profile on LinkedIn