Results 1 to 1 of 1
  1. #1
    featon is offline Member
    Join Date
    May 2010
    Posts
    1
    Rep Power
    0

    Default How to interact with Web page? ( HttpComponents)

    Hello. I'm interested in how to send the data or 'click' on the buttons using Java.
    I have found HttpComponents libraries. HttpComponents - HttpComponents Downloads. It seems to be the easiest way to do some interaction.

    Now I am using oldest version of this library (v3), and when I'm trying to send another 'Post' or 'Get' , web page seems to refresh, even when I didn't anything to close the connection.

    Java Code:
     int statusCode1 = client.executeMethod( method );
        
         int statusCode2 = client.executeMethod(jezyk );
         
    
         String contents = method.getResponseBodyAsString();
    The problem is, that I get void response from first Method, when I have executed second method.
    I want to get response from both method executions, and I want the web site, to remember both method executions, and then 'click' on Translate button.
    Full code:
    Java Code:
    // apache commons 3.1
    package http_java;
    import org.apache.commons.httpclient.*;
    import org.apache.commons.httpclient.methods.*;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    
    public class PostExample {
     public static void main( String[] args ) {
        String url = "http://babelfish.yahoo.com";
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod( url );
        HttpMethod jezyk = new GetMethod(url);
        HttpMethod OK = new GetMethod(url);
        NameValuePair nvp1= new NameValuePair("lp","en_de");
        NameValuePair nvp2= new NameValuePair("btnTrTxt","Translate");
    
       try {
    
    
    
        // Configure the form parameters
         method.addParameter( "trtext", "Hello, How are you? What is your name?" );
      // Execute the POST method
        int statusCode1 = client.executeMethod( method );
    
    
        if( statusCode1 != -1 ) {
                //String contents = method.getResponseBodyAsString();
              // System.out.println( contents );
                //System.out.println(method.getResponseBodyAsString());
                byte [] res  = method.getResponseBody();
     //write to file
                FileOutputStream fos= new FileOutputStream("donepage.html");
               fos.write(res);
                              }
       }
    //   }
       catch( Exception e ) {
        e.printStackTrace();
    
       }
    
                jezyk.setQueryString(new NameValuePair[]{nvp1});
        
         try{
               int statusCode2 = client.executeMethod(jezyk );
    
                System.out.println("QueryString>>> "+jezyk.getQueryString());
                System.out.println("Status Text>>>"
                      +HttpStatus.getStatusText(statusCode2));
    
                //Get data as a String
             //  System.out.println(jezyk.getResponseBodyAsString());
    
                //OR as a byte array
                byte [] res  = jezyk.getResponseBody();
    
    
                //write to file
                FileOutputStream fos= new FileOutputStream("donepage1.html");
                fos.write(res);
    
               
                
            }
          catch(IOException e) {
                e.printStackTrace();
            }
    
    
    	 
            
             OK.setQueryString(new NameValuePair[]{nvp2});
         try{
                int OKstatusCode = client.executeMethod(OK);
    
                System.out.println("QueryString>>> "+OK.getQueryString());
                System.out.println("Status Text>>>"
                      +HttpStatus.getStatusText(OKstatusCode));
    
                //Get data as a String
               
              if( (OKstatusCode    != -1) ) {
            //   String contents = method.getResponseBodyAsString();
             //  System.out.println(OK.getResponseBodyAsString());
    
                //OR as a byte array
                byte [] res  = OK.getResponseBody();
    
    
                //write to file
                FileOutputStream fos= new FileOutputStream("donepage2.html");
                fos.write(res);
    
          // !!!!!!!!!!!!!!!!!!!!!!//    method.releaseConnection();
              
              }
    
    
            }
          catch(IOException e) {
                e.printStackTrace();
            }
    
    
               
        }
       }
    //   THX!!
    Last edited by featon; 05-29-2010 at 04:03 PM.

Similar Threads

  1. JList of components you can interact with
    By Streagan in forum AWT / Swing
    Replies: 1
    Last Post: 03-06-2010, 11:26 PM
  2. Replies: 0
    Last Post: 09-02-2009, 03:28 PM
  3. Replies: 0
    Last Post: 08-31-2009, 09:09 PM
  4. Replies: 0
    Last Post: 04-12-2009, 04:16 PM
  5. Replies: 1
    Last Post: 11-18-2008, 04:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •