You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by Anu Kulatunga <an...@bea.com> on 2003/08/14 16:42:07 UTC

HTTPClient PostMethod - postinf form data

Please copy my address ( anurudha@bea.com) 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){}
}


Re: HTTPClient PostMethod - postinf form data

Posted by Michael Becke <be...@u.washington.edu>.
Odi,

Either should work.  The only difference is one adds and one overwrites.

Mike

On Friday, August 15, 2003, at 03:13 AM, Ortwin Glück wrote:

> Anu,
>
> you should use authpost.addParameters(new NameValuePair[] {action, 
> url})
>
> and not setRequestBody.
>
> HTH
>
> Odi
>
>>    NameValuePair action   = new NameValuePair("url", "Books");
>>   NameValuePair url      = new NameValuePair("field-keywords", 
>> "java");
>>   authpost.setRequestBody(new NameValuePair[] {action, url});
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: 
> commons-httpclient-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: 
> commons-httpclient-dev-help@jakarta.apache.org
>


Re: HTTPClient PostMethod - postinf form data

Posted by Ortwin Glück <or...@nose.ch>.
Anu,

you should use authpost.addParameters(new NameValuePair[] {action, url})

and not setRequestBody.

HTH

Odi

>  
>   NameValuePair action   = new NameValuePair("url", "Books");
>   NameValuePair url      = new NameValuePair("field-keywords", "java");
>   authpost.setRequestBody(new NameValuePair[] {action, url});


Re: HTTPClient PostMethod - postinf form data

Posted by Michael Becke <be...@u.washington.edu>.
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 ( anurudha@bea.com) 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){}
> }
>