The Source for Java Technology Collaboration
User: Password:



Simon Brown

Simon Brown's Blog

HttpClient - another great Jakarta Commons component

Posted by simongbrown on October 06, 2003 at 12:58 PM | Comments (9)

I was putting TrackBack support into Pebble the other day and the found that the technical details of a TrackBack involve sending a HTTP POST request to the remote server. I've implemented HTTP POSTs before using the classes in the java.net package, but rather than write all this code again, I thought that I'd take a look at Jakarta Commons HttpClient. What can I say ... this is another top notch component from the Commons project.

The following code shows how easy it is to make a HTTP POST with some name=value pairs and get the results back as a string.

      HttpClient httpClient = new HttpClient();
      PostMethod postMethod = new PostMethod(url);
      NameValuePair[] data = {
        new NameValuePair("name1", value1),
        new NameValuePair("namen", valuen)
      };
      postMethod.setRequestBody(data);
      int responseCode = httpClient.executeMethod(postMethod);
      String responseBody = postMethod.getResponseBodyAsString();

As you can see, there's not much code here at all, and HTTP GETs are easier still! From start to finish, making use of this component took about five, maybe ten, minutes. If you find you need to access HTTP-based resources, take a look at HttpClient.


Bookmark blog post: del.icio.us del.icio.us Digg Digg DZone DZone Furl Furl Reddit Reddit
Comments
Comments are listed in date ascending order (oldest first) | Post Comment

  • Best Starting Point
    The best starting point for HttpClient is to read the tutorial. It's available at http://jakarta.apache.org/commons/httpclient/tutorial.html and outlines the right pattern for HttpClient usage.

    HttpClient is pretty straight forward but there are a couple of unavoidable gotcha's in there, particularly when you get into the more advanced stuff.

    Posted by: ajsutton on October 06, 2003 at 02:49 PM

  • Good project
    I agree that HTTPClient is well worth using. However, it's quite a comprehensive package and is definitely overkill for simple applications. People should familiarize themselves with the java.net package first, and then decide whether they really need to add the 110 classes of HTTPClient to get their work done.

    Posted by: edfactor on October 07, 2003 at 09:10 AM

  • unless you're constrained by size use HttpClient
    I think the java.net classes are so broken when it comes to basic Web stuff, I always use HttpClient whenever I can. It's easy. It's quick. and the dev team is very helpful with questions.

    Posted by: mwilcox on October 07, 2003 at 02:06 PM

  • unless you're constrained by size use HttpClient
    I agree. Although I'm only using the tiniest portion of HttpClient and the JAR file is just over 200Kb, the time saved manipulating request headers, response codes and reading the response through an InputStream is more than enough justfication that I should use it. For me, HttpClient does exactly what I want a framework/component to do - it provides a simple interface and allows you to get work done quickly. edfactor does make a good point though - how big would the JAR file have to be before you considered not using it?

    Posted by: simongbrown on October 07, 2003 at 02:14 PM

  • Why NameValuePair?
    Why did they have to invent NameValuePair? Isn't that what Properties are for?

    Posted by: pat on October 07, 2003 at 06:06 PM

  • Why NameValuePair?
    NameValuePair encapsulates a single name / value pair. Properties are a collection however. There is no class in the standard API that could have been used here and we needed one. That's why.

    Ortwin Glück

    Posted by: oglueck on October 08, 2003 at 01:15 AM

  • Why won't this work?
    What if I want to log into a web site? I tried to use this method to POST to a 'j_security_check' form, but keep getting redirected to the login.jsp page. Does the password need to be encrypted or something? Code looks something like this:

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(url);
    NameValuePair[] data = {
    new NameValuePair("j_username", "user"),
    new NameValuePair("j_password", "password"),
    new NameValuePair("Login", "Login")
    };
    postMethod.setRequestBody(data);
    int responseCode = httpClient.executeMethod(postMethod);
    String responseBody = postMethod.getResponseBodyAsString();

    Here is the header I send after getting the cookie:
    User-Agent: Jakarta Commons-HttpClient/2.0.1
    Host: localhost
    Cookie: JSESSIONID=8A2E9C4757C0EAA806C0618AB1B89896
    Content-Length: 74
    Content-Type: application/x-www-form-urlencoded

    j_username=user&j_password=password&Login=Login

    And here is what the response header looks like:
    Content-Type: text/html;charset=ISO-8859-1
    Transfer-Encoding: chunked
    Date: Mon, 16 Aug 2004 17:31:42 GMT
    Server: Apache-Coyote/1.1

    Any ideas why this isn't working for me?
    Thanks


    Posted by: macraebr on August 16, 2004 at 03:35 AM

  • wow power leveling
    wow powerleveling
    wow power leveling
    wow gold
    wow items
    feelingame.com
    wow tips
    Most Valuable WOW Power Leveling Service
    wow power leveling faq
    cheap wow power leveling
    wow power leveling
    wow powerleveling
    wow power lvl

    Posted by: wowleveling3 on December 13, 2007 at 06:09 PM

  • 网络è¥é”€è½¯ä»¶
    网络è¥é”€è½¯ä»¶
    网络è¥é”€è½¯ä»¶
    群å‘软件
    群å‘软件
    ---
    群å‘软件
    网络è¥é”€è½¯ä»¶
    论å›ç¾¤å‘软件
    网站排å软件
    群å‘软件
    推广å°åŠ©æ‰‹ç ´è§£ç‰ˆ
    论å›ç¾¤å‘软件
    网站排å软件
    群å‘软件
    网络è¥é”€è½¯ä»¶
    网站推广软件
    ä¿¡æ¯ç¾¤å‘软件
    论å›ç¾¤å‘软件
    ä¿¡æ¯ç¾¤å‘软件
    åšå®¢ç¾¤å‘软件
    qq群å‘软件
    邮件群å‘软件
    åšå®¢ç¾¤å»ºè½¯ä»¶
    ä¼ä¸šå录æœç´¢è½¯ä»¶
    ä¿¡æ¯ç¾¤å‘软件
    邮件群å‘软件
    论å›ç¾¤å‘软件
    åšå®¢ç¾¤å‘软件
    网站推广软件
    网络è¥é”€è½¯ä»¶
    全能è¥é”€ç ´è§£ç‰ˆ

    Posted by: bininine on December 16, 2007 at 01:21 AM





Powered by
Movable Type 3.01D
 Feed java.net RSS Feeds