Return-Path: Delivered-To: apmail-commons-user-archive@www.apache.org Received: (qmail 35443 invoked from network); 25 Aug 2008 22:46:01 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 25 Aug 2008 22:46:01 -0000 Received: (qmail 43639 invoked by uid 500); 25 Aug 2008 22:45:56 -0000 Delivered-To: apmail-commons-user-archive@commons.apache.org Received: (qmail 43582 invoked by uid 500); 25 Aug 2008 22:45:55 -0000 Mailing-List: contact user-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Users List" Delivered-To: mailing list user@commons.apache.org Received: (qmail 43571 invoked by uid 99); 25 Aug 2008 22:45:55 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 15:45:55 -0700 X-ASF-Spam-Status: No, hits=2.0 required=10.0 tests=HTML_MESSAGE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: local policy) Received: from [192.118.49.221] (HELO ns7.comverse.com) (192.118.49.221) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 25 Aug 2008 22:44:55 +0000 X-SBRS: None X-IronPort-AV: E=Sophos;i="4.32,266,1217797200"; d="scan'208,217";a="74985787" X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----_=_NextPart_001_01C90704.1C096257" Subject: Help Debug a Get Request Date: Mon, 25 Aug 2008 16:44:16 -0600 Message-ID: <1127E556580A274B90153962447F9C0102019179@us-dnv-mail01.comverse.com> X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Help Debug a Get Request Thread-Index: AckHBBpejTbra64wRoqPsHZKxIRbew== From: "Murphy Steve" To: X-Virus-Checked: Checked by ClamAV on apache.org ------_=_NextPart_001_01C90704.1C096257 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable (Reposting this in case it was overlooked because it was a child of another thread.) My app has to go to the following web site: http://whois.domaintools.com/ The the app must programatically fill in a text field in a form and submit the form.=20 The value for the text field is: www.ezines.com The html for the form is this:
I'm using the code below, but the page returned is the original page rather than the page that is returned if I acutally go to the site, enter information in the text field, and manually submit the form. Two questions: 1) Based on the form definition above, are my parameter definitions correct? 2) Are there any other problems with my app? Thanks... 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=20 { private static String URL =3D "http://whois.domaintools.com/"; private static String domainToLookup =3D "www.ezines.com"; public static void main(String[] args) { System.out.println("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"); System.out.println("Starting App"); System.out.println("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"); Header responseHeaders[] =3D null; // Create an instance of HttpClient. HttpClient client =3D new HttpClient(); // Create a method instance. GetMethod method =3D new GetMethod(URL); method.setFollowRedirects(true); // Provide custom retry handler is necessary =09 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try=20 { //////////////////////////////////////////////// // 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 =3D client.executeMethod(method); if (statusCode !=3D HttpStatus.SC_OK)=20 { System.err.println("Method failed: " + method.getStatusLine()); } else { System.out.println("Method succeeded: " + 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(); =20 //////////////////////////////////////////////// =20 // Prepare parameters //////////////////////////////////////////////// //****************** OLD *********************** NameValuePair[] params =3D=20 { new NameValuePair("action", "http://www.domaintools.com/go/"), new NameValuePair("service", "whois"), new NameValuePair("q", domainToLookup) }; //****************** END OLD BLOCK ************* //*************TRY THIS:*********************************** method =3D new GetMethod( params[0].getValue()); //"http://www.domaintools.com/go/" =20 method.setFollowRedirects(true); // Provide custom retry handler is necessary =09 method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); =20 for ( int i=3D1; 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 =3D client.executeMethod( method); if (statusCode !=3D HttpStatus.SC_OK)=20 { System.out.println("Method failed: " + method.getStatusLine()); } else { System.out.println("Method succeeded: " + method.getStatusLine()); System.out.println("Status code: " + statusCode); //////////////////// //harvest response info, etc.... ////////////////////////// =09 System.out.println("--------------------------"); System.out.println("INFO"); System.out.println("Page contents:"); =09 System.out.println("--------------------------"); =09 printPageFromInputStream(method.getResponseBodyAsStream()); } //**************end of TRY THIS comment *************************** =20 } 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(); }=20 finally { // Release the connection. method.releaseConnection(); System.out.println("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"); System.out.println("App Finished"); System.out.println("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D"); =20 } } // END main() =20 public static void printPageFromInputStream(InputStream is) { byte[] b =3D new byte[50000]; int ret =3D 0; =20 int offset =3D 0; try=20 { while (ret !=3D -1) { ret =3D is.read(b, offset, 1); System.out.print( (char) b[offset]); offset++; } }=20 catch (IOException e)=20 { // TODO Auto-generated catch block e.printStackTrace(); } } // END printPageFromInputStream =20 }=20 ------_=_NextPart_001_01C90704.1C096257--