Anu,

The problem seems to be the lack of the content type header when setting the request body manually. This value is set when using the NameValuePairs since the method knows how the value are encoded. Either use setRequestBody(NameValuePair[]) or add the following line when setting the body as a string:

PostMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

Mike

On Thursday, August 14, 2003, at 10:42 AM, Anu Kulatunga wrote:

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){}
}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to