Thomas Sundberg

August 4, 2009

HttpComponents – a great tool with great examples based on a future release

Filed under: Java — Tags: , , , — Thomas Sundberg @ 14:43

This post is available at http://www.thinkcode.se/blog/2009/08/04/httpcomponents-a-great-tool-with-great-examples-based-on-a-future-release

5 Comments »

  1. Hey guy!
    Many thanks for the neat description of the problem..

    I just wasted 1-2 hours to find out the 30% of your infos but without any result..
    Your solution obviously work!

    Thanks again and good programming!

    Comment by Davide — September 2, 2009 @ 13:20

  2. I am coming up with some errors when I try to compile and run this program. Are there any prerequisites in order to run this? I am using Netbeans 6.7 and I have a web app with servlets and jsp pages. what should I do to make this work? I would like to use the HttpCore concept in my program.

    Comment by Duwhop — September 9, 2009 @ 18:14

    • There is one pre-requisite, the example is built using Maven and Maven will download any dependencies. You don’t state the nature of your compilation error so I can’t say much more then that. Could it be that you are missing any dependencies?

      Comment by Thomas Sundberg — September 9, 2009 @ 19:44

  3. Thank you Thomas for your reply. I am not using maven (I never used it before…therefore my project was not set up to use it.). I did, however get it working. I used some of your code and some of the original code fixed some of my errors. My code now looks like this (not much difference):
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, “UTF-8”);
    HttpProtocolParams.setUserAgent(params, “HttpComponents/1.1”);
    HttpProtocolParams.setUseExpectContinue(params, true);

    BasicHttpProcessor httpproc = new BasicHttpProcessor(/*new HttpRequestInterceptor[]*/); {
    // Required protocol interceptors
    httpproc.addInterceptor(new RequestContent());
    httpproc.addInterceptor(new RequestTargetHost());
    // Recommended protocol interceptors
    httpproc.addInterceptor(new RequestConnControl());
    httpproc.addInterceptor(new RequestUserAgent());
    httpproc.addInterceptor(new RequestExpectContinue());

    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();

    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost(“geni.myvnc.com”, 8084);

    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();

    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);

    try {

    String[] targets = {
    // “/index.jsp”,
    “/GeniClearinghouse/index.jsp”/*,
    “/TestCMS/index.jsp”*/};

    for (int i = 0; i > Request URI: ” + requ.getRequestLine().getUri());
    requ.setParams(params);
    httpexecutor.preProcess(requ, httpproc, context);
    HttpResponse resp = httpexecutor.execute(requ, conn, context);
    resp.setParams(params);
    httpexecutor.postProcess(resp, httpproc, context);

    System.out.println(“<< Response: " + resp.getStatusLine());
    System.out.println(EntityUtils.toString(resp.getEntity()));
    System.out.println("==============");
    HttpSession ses = request.getSession();
    ses.setAttribute("siteHttp", resp.getEntity());
    //request.setAttribute("siteHttp", siteHttp);
    if (!connStrategy.keepAlive(resp, context)) {
    conn.close();
    } else {
    System.out.println("Connection kept alive…");
    }
    }
    } finally {
    conn.close();
    }
    }
    It works! Thank you so much for posting this. I am sure I would have been much more lost than I was. side note: do you know how to mavenize a project in netbeans 6.7 (if you do and it helps I am using tomcat 6.0, apache 2.2 (this is to send requests to tomcat via ajp), if you don't then scratch everything that was stated in this parenthesis and I will leave the project as is). Thank you!

    Comment by Duwhop — September 10, 2009 @ 16:43

    • I’m glad that it works for you. You could convert a project from using Netbeans to Maven, I would suggest that you take a look at http://maven.apache.org/

      This is an approach I would suggest most of the time since it separates the development environment from the build environment and thus enables you to allow all developers to use there favourite editor and still be able to take advantages of Mavens dependency handling, continuous integration etc.

      My personal development environment favourite is Intellij IDEA, but another developer could use emacs or any other editor and still build the project in a standard way.

      I Googled on Maven and Netbeans and found this: http://maven.apache.org/netbeans-module.html

      Comment by Thomas Sundberg — September 11, 2009 @ 06:51


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: