Blog Archive

Java HTTP post with parameters using org.apache.commons.httpclient

Many applications need to simulate the process of submitting an HTML form, for instance, in order to log in to a web application or submit input data. HttpClient provides the entity class UrlEncodedFormEntity to facilitate the process.
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);

The UrlEncodedFormEntity instance will use the so called URL encoding to encode parameters and produce the following content:
 
param1=value1&param2=value2


You need the following jars for the sample java code to work
  • httpcore-4.1.jar
  • httpclient-4.1.1.jar
  • commons-logging-1.1.1.jar
Click here to download HttpClient from Apache Commons

Sample Java code for Http post with Parameters

package com.as400samplecode;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


public class RequestParamExample {

   
    public static void main(String[] args)
    {
        HttpParameterPost();

    }

    private static void  HttpParameterPost() {

        HttpClient httpclient = new DefaultHttpClient();

        try {

            HttpPost httpPost = new HttpPost("http://localhost:8080/examples/servlets/servlet/RequestParamExample");

            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("firstname","as400"));
            nameValuePairs.add(new BasicNameValuePair("lastname","samplecode"));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

            System.out.println("executing request " + httpPost.getRequestLine());
            HttpResponse response = httpclient.execute(httpPost);
            HttpEntity resEntity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            if (resEntity != null) {
                System.out.println("Response content length: " + resEntity.getContentLength());
                System.out.println("Chunked?: " + resEntity.isChunked());
                String responseBody = EntityUtils.toString(resEntity);
                System.out.println("Data: " + responseBody);
            }
           
           
            EntityUtils.consume(resEntity);
        } 
        catch (Exception e) {
            System.out.println(e);
        }
        finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }



    }

}