tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons Http Client > How to create Dynamic Entity

How to create Dynamic Entity 

Commons Http Client is a HTTP agent implementation in java supporting client-side authentication, HTTP state management and HTTP connection management. This requires the libraries httpclient-4.1.2.jar, httpcore-4.1.2.jar, httpmime-4.1.2.jar, httpclient-cache-4.1.2.jar, commons-codec.jar and commons-logging-1.1.1.jar to be in classpath. The following example shows how to create dynamic entity through HTTP Client.

File Name  :  
Author  :  Sudhakar KV
Email  :  [email protected]
package com.bethecoder.tutorials.commons_httpclient;


import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class DynamicEntityTest {

   @param args
  public static void main(String[] args) {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:8080/HTTP_TEST_APP/print_request_body.jsp");
    System.out.println("Requesting : " + httppost.getURI());

    try {
      ContentProducer contentProducer = new ContentProducer() {
        public void writeTo(OutputStream outstreamthrows IOException {
          Writer writer = new OutputStreamWriter(outstream, "UTF-8");
          writer.write("<message>BETHECODER Tutorials</message>");
      HttpEntity entity = new EntityTemplate(contentProducer);

      ResponseHandler<String> responseHandler = new BasicResponseHandler();
      String responseBody = httpclient.execute(httppost, responseHandler);

      System.out.println("responseBody : " + responseBody);

    catch (UnsupportedEncodingException e) {
    catch (ClientProtocolException e) {
    catch (IOException e) {
    finally {


A simple Web Application which prints the request body through JSP.

File Name  :  

It gives the following output,
File Name  :  OUTPUT


bl  br