Results 1 to 1 of 1
- 05-28-2010, 11:58 PM #1
Member
- Join Date
- May 2010
- Posts
- 1
- Rep Power
- 0
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.
The problem is, that I get void response from first Method, when I have executed second method.Java Code:int statusCode1 = client.executeMethod( method ); int statusCode2 = client.executeMethod(jezyk ); String contents = method.getResponseBodyAsString();
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
-
JList of components you can interact with
By Streagan in forum AWT / SwingReplies: 1Last Post: 03-06-2010, 11:26 PM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs OfferedReplies: 0Last Post: 09-02-2009, 03:28 PM -
Meet and Interact with Top Recruiters and Recruiting companies of USA.
By esurvey in forum Jobs DiscussionReplies: 0Last Post: 08-31-2009, 09:09 PM -
How to interact with Windows Task Scheduler within Java Application
By tiburblium in forum New To JavaReplies: 0Last Post: 04-12-2009, 04:16 PM -
How to get objects to interact without inheritence in separate classes?
By Fliz in forum New To JavaReplies: 1Last Post: 11-18-2008, 04:48 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks