commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "J.-F. Rompre" <jrom...@gmail.com>
Subject Re: How Add Form Parameters to a Get Request
Date Sun, 24 Aug 2008 00:18:29 GMT
It seems like there is not much missing - only to connect your form info
with the request, then fire off another request - using setParameter( name,
value) instead of NameValuePairs (only used with PostMethod), so I would try
(my changes commented):

import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class QuestionToPost {
         private static String URL = "http://whois.domaintools.com//";

         private static String domainToLookup = "www.ezines.com";

         public static void main(String[] args)
         {
               Header responseHeaders[] = null;
           // Create an instance of HttpClient.
           HttpClient client = new HttpClient();
           // Create a method instance.
           GetMethod method = new GetMethod(URL);
           method.setFollowRedirects(true);
           // Provide custom retry handler is necessary

method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                       new DefaultHttpMethodRetryHandler(3, false));

           try
           {
               ////////////////////////////////////////////////
               // Get the page with the form
               ////////////////////////////////////////////////
               // Execute the method.
             int statusCode = client.executeMethod(method);

             if (statusCode != HttpStatus.SC_OK) {
               System.err.println("Method failed: " +
method.getStatusLine());
             }
             responseHeaders = method.getResponseHeaders();
             ////////////////////////////////////////////////
         // Prepare parameters
         ////////////////////////////////////////////////
         NameValuePair[] params =
         {
                 new NameValuePair("action",
"http://www.domaintools.com/go/"),
                 new NameValuePair("service", "whois"),
                 new NameValuePair("q", domainToLookup)
         };

         // What do I do next?

//*************TRY THIS:***********************************
    method = new GetMethod( params[0].getValue()); //"
http://www.domaintools.com/go/"
    for ( int i=1; i < params.length; i++)
        method.getParams().setParameter( params[i].getName(),
params[i].getValue());

    client.execute( method);
    ////////////////////
    //harvest response info, etc....
    //////////////////////////
//**************end of TRY THIS comment ***************************

           } catch (HttpException e) {
             System.err.println("Fatal protocol violation: " +
e.getMessage());
             e.printStackTrace();
           } catch (IOException e) {
             System.err.println("Fatal transport error: " +
e.getMessage());
             e.printStackTrace();
           }

           finally {
             // Release the connection.
             method.releaseConnection();
           }
         }
       }


On Sat, Aug 23, 2008 at 2:51 PM, Murphy Steve
<Stephen.Murphy@comverse.com>wrote:

> Please excuse this horrible posting. I'm completely new to commons
> HttpClient and I'm struggling.
>
> I'm using commons HttpClient to post a get request to a page that has a
> form. I'm following instructions on the programming primer at
> http://hc.apache.org/httpcomponents-client/primer.html
>
> The page has the followng definition for its form:
>
> <form action="http://www.domaintools.com/go/" method="get">
> <input type="hidden" name="service" value="whois" />
> <input name="q" type="text"  value="" maxlength="60" size="40"
> style="font-size: 256%"/>
> <input type="submit"  value="Lookup" size="40" style="font-size: 206%"
> />
>
> So I'm creating parameters like so:
> private static String domainToLookup = "www.ezines.com";
> NameValuePair[] params =
> {
>  new NameValuePair("action", "http://www.domaintools.com/go/"),
>  new NameValuePair("service", "whois"),
>  new NameValuePair("q", domainToLookup)
> };
>
> Question 1: Is this the correct way to contstruct form parameters?
> Question 2: Now that I've defined parameters I need to somehow add them
> to my request. How do I do this?
>
> Are there any code samples this scenario?
>
> Below is a listing of my shameful hack:
>
> import java.io.IOException;
> import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
> import org.apache.commons.httpclient.Header;
> import org.apache.commons.httpclient.HttpClient;
> import org.apache.commons.httpclient.HttpException;
> import org.apache.commons.httpclient.HttpStatus;
> import org.apache.commons.httpclient.NameValuePair;
> import org.apache.commons.httpclient.methods.GetMethod;
> import org.apache.commons.httpclient.params.HttpMethodParams;
>
> public class QuestionToPost {
>          private static String URL = "http://whois.domaintools.com//";
>
>          private static String domainToLookup = "www.ezines.com";
>
>          public static void main(String[] args)
>          {
>                Header responseHeaders[] = null;
>            // Create an instance of HttpClient.
>            HttpClient client = new HttpClient();
>            // Create a method instance.
>            GetMethod method = new GetMethod(URL);
>            method.setFollowRedirects(true);
>            // Provide custom retry handler is necessary
>
> method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
>                        new DefaultHttpMethodRetryHandler(3, false));
>
>            try
>            {
>                ////////////////////////////////////////////////
>                // Get the page with the form
>                ////////////////////////////////////////////////
>                // Execute the method.
>              int statusCode = client.executeMethod(method);
>
>              if (statusCode != HttpStatus.SC_OK) {
>                System.err.println("Method failed: " +
> method.getStatusLine());
>              }
>              responseHeaders = method.getResponseHeaders();
>              ////////////////////////////////////////////////
>          // Prepare parameters
>          ////////////////////////////////////////////////
>          NameValuePair[] params =
>          {
>                  new NameValuePair("action",
> "http://www.domaintools.com/go/"),
>                  new NameValuePair("service", "whois"),
>                  new NameValuePair("q", domainToLookup)
>          };
>
>          // What do I do next?
>
>            } catch (HttpException e) {
>              System.err.println("Fatal protocol violation: " +
> e.getMessage());
>              e.printStackTrace();
>            } catch (IOException e) {
>              System.err.println("Fatal transport error: " +
> e.getMessage());
>              e.printStackTrace();
>            }
>
>            finally {
>              // Release the connection.
>              method.releaseConnection();
>            }
>          }
>        }
>
> Thanks.
> Steve-O
>
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message