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
