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/26 00:44:16 UTC

Help Debug a Get Request

(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. 
The value for the text field is: www.ezines.com

The html for the form is this:

<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%"
/>
</td>
</tr>
</table></form>

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 
{
	  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());
	      }
	      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();
	      	      
	      ////////////////////////////////////////////////          
          // 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
		    {
		    	System.out.println("Method succeeded: " +
method.getStatusLine());
		    	System.out.println("Status code: " +
statusCode);
		    	////////////////////
		    	//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	  
}