You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by boy456 <ro...@luukku.com> on 2008/10/15 13:09:21 UTC

How to handle two forms on one page with apache httpclient

Dear friends

I would like to ask about using Apache httpclient for retrieving knowledge
from some online databases through forms.
My problem is that there is one webpage that contains two forms on one page
and I want to send only the second form, not the first one! Both forms have
a search box and submit button.
I thought that it might be enought to leave the NameValuePair parameters for
the first form just blank. But the code below does not work.

Thanks for helping.


import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
 
public class SubmitHttpForm {
 
    private static String url =
         "http://www.website.com";
 
    public static void main(String[] args) {
 
        //Instantiate an HttpClient
        HttpClient client = new HttpClient(new
MultiThreadedHttpConnectionManager());
 
        //Instantiate a GET HTTP method
        HttpMethod method = new GetMethod(url);
 
        //Define name-value pairs to set into the QueryString
        NameValuePair nvp1= new NameValuePair("search","");
        NameValuePair nvp2= new NameValuePair("submit","");
       NameValuePair nvp3= new NameValuePair("search","route");
       NameValuePair nvp3= new NameValuePair("submit","go");
 
      method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3, nvp4});
        
        try{
            int statusCode = client.executeMethod(method);
 
            System.out.println("QueryString>>> "+method.getQueryString());
            System.out.println("Status Text>>>"
                  +HttpStatus.getStatusText(statusCode));
 
            //Get data as a String
            System.out.println(method.getResponseBodyAsString());
 
            //OR as a byte array
            byte [] res  = method.getResponseBody();
 
            //write to file
            FileOutputStream fos= new
FileOutputStream("./output/donepage.html");
            fos.write(res);
 
            //release connection
            method.releaseConnection();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

 

-- 
View this message in context: http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19991354.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to handle two forms on one page with apache httpclient

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Wed, 2008-10-15 at 08:07 -0700, boy456 wrote:

> Please if you just can provide me a small example of the parameter
> definitions that I should use.
> Thank you very much!
> 
> 

Please make sure you have read this:

http://hc.apache.org/httpcomponents-client/primer.html

Oleg


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to handle two forms on one page with apache httpclient

Posted by sebb <se...@gmail.com>.
On 15/10/2008, boy456 <ro...@luukku.com> wrote:
>
>  Thank you very much for your help. I understand that there are some other
>  parameters available concerning the two different forms on the page. I have
>  tried to offer different combinations of paramters but I don't understand
>  the logic how to prevent the first form to capture the submit parameter
>  (they both have exactly same submit parameter).
>
>  Is it really a correct approach to define all possible parameters for both
>  forms? How should I define the parameters of the first form so that they
>  would not be taken into consideration? I have tried null and "" with no
>  success. Is there any command that can force only the parameters of the
>  second form to be activated? I am very thankful if you can clarify for me
>  the logic.
>
>  I am going to use "Wikipedia what links here" page that provides to search
>  boxes. I would like to send ONLY the second form of these to that appear on
>  the page:
>
>  <form action="/wiki/Special:Search" id="searchform"><div>

Compare the action ...

>  <input id="searchInput" name="search" type="text" title="Search Wikipedia
>  [f]" accesskey="f" value="" />
>  <input type='submit' name="go" class="searchButton" id="searchGoButton"
>  value="Go" title="Go to a page with this exact name if one exists" />&nbsp;
>  <input type='submit' name="fulltext" class="searchButton"
>  id="mw-searchButton" value="Search" title="Search Wikipedia for this text"
>  />
>  </div></form>
>
>  <form action="/w/index.php"><input name="title" type="hidden"

... with this action.

Notice any difference?

>  value="Special:WhatLinksHere" /><input name="limit" type="hidden"
>  value="500" /><input name="hideredirs" type="hidden" value="1" /><input
>  name="hidetrans" type="hidden" value="1" /><fieldset>
>  <legend>What links here</legend>
>  <label for="mw-whatlinkshere-target">Page:</label>&nbsp;<input name="target"
>  size="40" value="Eno" id="mw-whatlinkshere-target" /> <label
>  for="namespace">Namespace:</label>&nbsp;<select id="namespace"
>  name="namespace" class="namespaceselector">
>  <option value="" selected="selected">all</option>
>  <option value="0">(Article)</option>
>  <option value="1">Talk</option>
>  <option value="2">User</option>
>  </select><input type="submit" value="Go" /></fieldset></form>
>
>  Please if you just can provide me a small example of the parameter
>  definitions that I should use.
>  Thank you very much!
>
>
>
>
>  sebb-2-2 wrote:
>  >
>  > On 15/10/2008, boy456 <ro...@luukku.com> wrote:
>  >
>
> > There must be a difference between the forms, or the server will not
>  > be able to distinguish which one has been used.
>  >
>  > Either there is a different parameter value, or, more likely, the
>  > Submit URL is different.
>  >
>  >>  Thanks for helping.
>  >>
>  >>
>  >>  import java.io.FileOutputStream;
>  >>  import java.io.IOException;
>  >>  import org.apache.commons.httpclient.*;
>  >>  import org.apache.commons.httpclient.methods.GetMethod;
>  >>
>  >>  public class SubmitHttpForm {
>  >>
>  >>     private static String url =
>  >>          "http://www.website.com";
>  >>
>  >>     public static void main(String[] args) {
>  >>
>  >>         //Instantiate an HttpClient
>  >>         HttpClient client = new HttpClient(new
>  >>  MultiThreadedHttpConnectionManager());
>  >>
>  >>         //Instantiate a GET HTTP method
>  >>         HttpMethod method = new GetMethod(url);
>  >>
>  >>         //Define name-value pairs to set into the QueryString
>  >>         NameValuePair nvp1= new NameValuePair("search","");
>  >>         NameValuePair nvp2= new NameValuePair("submit","");
>  >>        NameValuePair nvp3= new NameValuePair("search","route");
>  >>        NameValuePair nvp3= new NameValuePair("submit","go");
>  >>
>  >>       method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3, nvp4});
>  >>
>  >>         try{
>  >>             int statusCode = client.executeMethod(method);
>  >>
>  >>             System.out.println("QueryString>>>
>  >> "+method.getQueryString());
>  >>             System.out.println("Status Text>>>"
>  >>                   +HttpStatus.getStatusText(statusCode));
>  >>
>  >>             //Get data as a String
>  >>             System.out.println(method.getResponseBodyAsString());
>  >>
>  >>             //OR as a byte array
>  >>             byte [] res  = method.getResponseBody();
>  >>
>  >>             //write to file
>  >>             FileOutputStream fos= new
>  >>  FileOutputStream("./output/donepage.html");
>  >>             fos.write(res);
>  >>
>  >>             //release connection
>  >>             method.releaseConnection();
>  >>         }
>  >>         catch(IOException e) {
>  >>             e.printStackTrace();
>  >>         }
>  >>     }
>  >>  }
>  >>
>  >>
>  >>
>  >>
>  >>  --
>  >>  View this message in context:
>  >> http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19991354.html
>  >>  Sent from the HttpClient-User mailing list archive at Nabble.com.
>  >>
>  >>
>  >>  ---------------------------------------------------------------------
>  >>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  >>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>  >>
>  >>
>  >
>  > ---------------------------------------------------------------------
>  > To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  > For additional commands, e-mail: httpclient-users-help@hc.apache.org
>  >
>  >
>  >
>
>
> --
>  View this message in context: http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19995478.html
>
> Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to handle two forms on one page with apache httpclient

Posted by boy456 <ro...@luukku.com>.
Thank you very much for your help. I understand that there are some other
parameters available concerning the two different forms on the page. I have
tried to offer different combinations of paramters but I don't understand
the logic how to prevent the first form to capture the submit parameter
(they both have exactly same submit parameter). 

Is it really a correct approach to define all possible parameters for both
forms? How should I define the parameters of the first form so that they
would not be taken into consideration? I have tried null and "" with no
success. Is there any command that can force only the parameters of the
second form to be activated? I am very thankful if you can clarify for me
the logic. 

I am going to use "Wikipedia what links here" page that provides to search
boxes. I would like to send ONLY the second form of these to that appear on
the page:

<form action="/wiki/Special:Search" id="searchform"><div>
<input id="searchInput" name="search" type="text" title="Search Wikipedia
[f]" accesskey="f" value="" />
<input type='submit' name="go" class="searchButton" id="searchGoButton"
value="Go" title="Go to a page with this exact name if one exists" />&nbsp;
<input type='submit' name="fulltext" class="searchButton"
id="mw-searchButton" value="Search" title="Search Wikipedia for this text"
/>
</div></form>

<form action="/w/index.php"><input name="title" type="hidden"
value="Special:WhatLinksHere" /><input name="limit" type="hidden"
value="500" /><input name="hideredirs" type="hidden" value="1" /><input
name="hidetrans" type="hidden" value="1" /><fieldset>
<legend>What links here</legend>
<label for="mw-whatlinkshere-target">Page:</label>&nbsp;<input name="target"
size="40" value="Eno" id="mw-whatlinkshere-target" /> <label
for="namespace">Namespace:</label>&nbsp;<select id="namespace"
name="namespace" class="namespaceselector">
<option value="" selected="selected">all</option>
<option value="0">(Article)</option>
<option value="1">Talk</option>
<option value="2">User</option>
</select><input type="submit" value="Go" /></fieldset></form>

Please if you just can provide me a small example of the parameter
definitions that I should use.
Thank you very much!



sebb-2-2 wrote:
> 
> On 15/10/2008, boy456 <ro...@luukku.com> wrote:
> 
> There must be a difference between the forms, or the server will not
> be able to distinguish which one has been used.
> 
> Either there is a different parameter value, or, more likely, the
> Submit URL is different.
> 
>>  Thanks for helping.
>>
>>
>>  import java.io.FileOutputStream;
>>  import java.io.IOException;
>>  import org.apache.commons.httpclient.*;
>>  import org.apache.commons.httpclient.methods.GetMethod;
>>
>>  public class SubmitHttpForm {
>>
>>     private static String url =
>>          "http://www.website.com";
>>
>>     public static void main(String[] args) {
>>
>>         //Instantiate an HttpClient
>>         HttpClient client = new HttpClient(new
>>  MultiThreadedHttpConnectionManager());
>>
>>         //Instantiate a GET HTTP method
>>         HttpMethod method = new GetMethod(url);
>>
>>         //Define name-value pairs to set into the QueryString
>>         NameValuePair nvp1= new NameValuePair("search","");
>>         NameValuePair nvp2= new NameValuePair("submit","");
>>        NameValuePair nvp3= new NameValuePair("search","route");
>>        NameValuePair nvp3= new NameValuePair("submit","go");
>>
>>       method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3, nvp4});
>>
>>         try{
>>             int statusCode = client.executeMethod(method);
>>
>>             System.out.println("QueryString>>>
>> "+method.getQueryString());
>>             System.out.println("Status Text>>>"
>>                   +HttpStatus.getStatusText(statusCode));
>>
>>             //Get data as a String
>>             System.out.println(method.getResponseBodyAsString());
>>
>>             //OR as a byte array
>>             byte [] res  = method.getResponseBody();
>>
>>             //write to file
>>             FileOutputStream fos= new
>>  FileOutputStream("./output/donepage.html");
>>             fos.write(res);
>>
>>             //release connection
>>             method.releaseConnection();
>>         }
>>         catch(IOException e) {
>>             e.printStackTrace();
>>         }
>>     }
>>  }
>>
>>
>>
>>
>>  --
>>  View this message in context:
>> http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19991354.html
>>  Sent from the HttpClient-User mailing list archive at Nabble.com.
>>
>>
>>  ---------------------------------------------------------------------
>>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19995478.html
Sent from the HttpClient-User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Re: How to handle two forms on one page with apache httpclient

Posted by sebb <se...@gmail.com>.
On 15/10/2008, boy456 <ro...@luukku.com> wrote:
>
>  Dear friends
>
>  I would like to ask about using Apache httpclient for retrieving knowledge
>  from some online databases through forms.
>  My problem is that there is one webpage that contains two forms on one page
>  and I want to send only the second form, not the first one! Both forms have
>  a search box and submit button.
>  I thought that it might be enought to leave the NameValuePair parameters for
>  the first form just blank. But the code below does not work.

There must be a difference between the forms, or the server will not
be able to distinguish which one has been used.

Either there is a different parameter value, or, more likely, the
Submit URL is different.

>  Thanks for helping.
>
>
>  import java.io.FileOutputStream;
>  import java.io.IOException;
>  import org.apache.commons.httpclient.*;
>  import org.apache.commons.httpclient.methods.GetMethod;
>
>  public class SubmitHttpForm {
>
>     private static String url =
>          "http://www.website.com";
>
>     public static void main(String[] args) {
>
>         //Instantiate an HttpClient
>         HttpClient client = new HttpClient(new
>  MultiThreadedHttpConnectionManager());
>
>         //Instantiate a GET HTTP method
>         HttpMethod method = new GetMethod(url);
>
>         //Define name-value pairs to set into the QueryString
>         NameValuePair nvp1= new NameValuePair("search","");
>         NameValuePair nvp2= new NameValuePair("submit","");
>        NameValuePair nvp3= new NameValuePair("search","route");
>        NameValuePair nvp3= new NameValuePair("submit","go");
>
>       method.setQueryString(new NameValuePair[]{nvp1,nvp2, nvp3, nvp4});
>
>         try{
>             int statusCode = client.executeMethod(method);
>
>             System.out.println("QueryString>>> "+method.getQueryString());
>             System.out.println("Status Text>>>"
>                   +HttpStatus.getStatusText(statusCode));
>
>             //Get data as a String
>             System.out.println(method.getResponseBodyAsString());
>
>             //OR as a byte array
>             byte [] res  = method.getResponseBody();
>
>             //write to file
>             FileOutputStream fos= new
>  FileOutputStream("./output/donepage.html");
>             fos.write(res);
>
>             //release connection
>             method.releaseConnection();
>         }
>         catch(IOException e) {
>             e.printStackTrace();
>         }
>     }
>  }
>
>
>
>
>  --
>  View this message in context: http://www.nabble.com/How-to-handle-two-forms-on-one-page-with-apache-httpclient-tp19991354p19991354.html
>  Sent from the HttpClient-User mailing list archive at Nabble.com.
>
>
>  ---------------------------------------------------------------------
>  To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
>  For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org