You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Murphy Steve <St...@comverse.com> on 2008/08/23 20:51:40 UTC
How Add Form Parameters to a Get Request
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
RE: How Add Form Parameters to a Get Request
Posted by Murphy Steve <St...@comverse.com>.
Hi J.F.,
I made your changes and also added some informational displays. Also, I
had to close the connection after the first method call because I got
warning messages about SimpleHttpConnectionManager being used
incorrectly.
I reran the app, but the page returned from the second method call is
the same as the first. Is there another change I have to make? As I
mentioned, the form on the page is:
<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%"
/>
Thanks very much!
Following is my revised source.
import java.io.IOException;
import java.io.InputStream;
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)
{
System.out.println("============");
System.out.println("Starting App");
System.out.println("============");
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.
System.out.println("--------------------------");
System.out.println("INFO");
System.out.println("Getting " + URL + " page.");
System.out.println("--------------------------");
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK)
{
System.err.println("Method failed: " +
method.getStatusLine());
}
////////////////////////////////////////////////
// Close old connection to eliminate warning message
////////////////////////////////////////////////
System.out.println("--------------------------");
System.out.println("INFO");
System.out.println("Closing old connection.");
System.out.println("--------------------------");
method.releaseConnection();
////////////////////////////////////////////////
// Prepare parameters
////////////////////////////////////////////////
//****************** OLD ***********************
NameValuePair[] params =
{
new NameValuePair("action",
"http://www.domaintools.com/go/"),
new NameValuePair("service", "whois"),
new NameValuePair("q", domainToLookup)
};
//****************** END OLD BLOCK *************
//*************TRY
THIS:***********************************
method = new GetMethod( params[0].getValue());
//"http://www.domaintools.com/go/"
method.setFollowRedirects(true);
// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));
for ( int i=1; i < params.length; i++)
method.getParams().setParameter( params[i].getName(),
params[i].getValue());
System.out.println("--------------------------");
System.out.println("INFO");
System.out.println("Posting form");
System.out.println("--------------------------");
statusCode = client.executeMethod( method);
if (statusCode != HttpStatus.SC_OK)
{
System.out.println("Method failed: " +
method.getStatusLine());
}
else
{
////////////////////
//harvest response info, etc....
//////////////////////////
System.out.println("--------------------------");
System.out.println("INFO");
System.out.println("Page contents:");
System.out.println("--------------------------");
printPageFromInputStream(method.getResponseBodyAsStream());
}
//**************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();
System.out.println("============");
System.out.println("App Finished");
System.out.println("============");
}
} // END main()
public static void printPageFromInputStream(InputStream is)
{
byte[] b = new byte[50000];
int ret = 0;
int offset = 0;
try
{
while (ret != -1)
{
ret = is.read(b, offset, 1);
System.out.print( (char) b[offset]);
offset++;
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
} // END printPageFromInputStream
}
-----Original Message-----
From: J.-F. Rompre [mailto:jrompre@gmail.com]
Sent: Saturday, August 23, 2008 6:18 PM
To: Commons Users List
Subject: Re: How Add Form Parameters to a Get Request
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
<St...@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
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: How Add Form Parameters to a Get Request
Posted by "J.-F. Rompre" <jr...@gmail.com>.
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
<St...@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
>
>
>