For the benefit of someone that may search the archives : I figured out why mine does 
not work.
The content type header needs to be set as follows :
 
authpost.setRequestHeader("Content-Type", authpost.FORM_URL_ENCODED_CONTENT_TYPE);
BEFORE setRequestBody is called....

-----Original Message-----
From: Anu Kulatunga 
Sent: Thursday, August 14, 2003 10:45 AM
To: Roland Weber; Commons HttpClient Project
Subject: HTTPClient PostMethod - postinf form data


Please copy my address ( [EMAIL PROTECTED]) in your response as I am not a 
sunbbscriber to this list yet.
 
I an trying to invoke a HTTP POST to amazon.com.
 
The form has 2 parameters, url=Books and field-keywords.
 
If I use the following the call works fine :
 
  NameValuePair action   = new NameValuePair("url", "Books");
  NameValuePair url      = new NameValuePair("field-keywords", "java");
  authpost.setRequestBody(new NameValuePair[] {action, url});
 
If I use the setRequestBody that takes in a String the website reports that no data 
was found.
 
In other words the follwoing does not work :
  authpost.setRequestBody("url=Books&field-keywords=java");
 
And neither does this .... (I looked that the source code for the PostMethod).
  authpost.setRequestBody(EncodingUtil.formUrlEncode((new NameValuePair[] {action, 
url}), authpost.getRequestCharSet()));
 
Please help me understand why I cannot pass a string like 
"url=Books&field-keywords=java" to the PostMethod sucessfully.
 
My class is attached herewith:
 


import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.cookie.CookiePolicy;
import org.apache.commons.httpclient.cookie.CookieSpec;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.util.EncodingUtil;
import java.io.*;
 

public class AkshayDemo2
{
    static final String LOGON_SITE = " www.amazon.com";
    static final int    LOGON_PORT = 80;
 
    public AkshayDemo2() {
        super();
    }
 
    public static void main(String[] args) throws Exception {
 
        HttpClient client = new HttpClient();
        client.getHostConfiguration().setHost(LOGON_SITE, LOGON_PORT, "http");
        PostMethod authpost = new 
PostMethod("/exec/obidos/search-handle-form/002-6745494-8676845");
 

  /*
  // This block works fine
  NameValuePair action   = new NameValuePair("url", "Books");
  NameValuePair url      = new NameValuePair("field-keywords", "java");
        authpost.setRequestBody(new NameValuePair[] {action, url});
  */
 

  // None of these work !!!!!!!!!!!!
  //authpost.setRequestBody("url=Books&field-keywords=java");
  authpost.setRequestBody(EncodingUtil.formUrlEncode((new NameValuePair[] {action, 
url}), authpost.getRequestCharSet()));
 

  System.out.println("Request -->"+authpost.getRequestBodyAsString()+"<-- ");
  System.out.println("Headers --> BEGIN ");
        Header[] requestHeaders = authpost.getRequestHeaders();
        for (int i=0; i<requestHeaders.length; i++){
            System.out.print(requestHeaders[i]);
        }
  System.out.println("Headers --> END ");
 

        client.executeMethod(authpost);
        System.out.println("Login form post: " + authpost.getStatusLine().toString());
        // release any connection resources used by the method
        int statuscode = authpost.getStatusCode();
        System.out.println("Status code = "+statuscode);
        if (statuscode==200) showResponse(authpost);
        authpost.releaseConnection();
    }
 
 private static void showResponse(HttpMethodBase method){
  System.out.println("Response Body ---------------- BEGIN");
  System.out.println(method.getResponseBodyAsString());
  System.out.println("Response Body ---------------- END");
 }
 
    private static void displayHTML(String html){}
}


Reply via email to