Alternatives to Apache HttpComponents?

26,165

Solution 1

Answering my own question since this got resurrected for some reason.

I ended up writing a few simple wrappers around java.net.HttpURLConnection, seems it's come a long way since the last time I seriously considered it.

Apache HttpComponents is great, but can be overkill for simple tasks. Also, at least in my scenario, HUC is noticeably faster (mostly single-threaded, haven't done any testing under heavy load).

Solution 2

Complexity of HttpClient API simply reflects the complexity of its problem domain. Contrary to a popular misconception HTTP is a fairly complex protocol. Being a low level transport library HC 4.0 API was primarily optimized for performance and flexibility rather than simplicity. It is regrettable that you are not able to figure it out, but so be it. You are welcome to use whatever library that suits your needs best. I personally like Jetty HttpClient a lot. It is a great alternative that might work better for you.

Solution 3

For simple use cases you can use HttpClient Fluent API. See tutorials.

This module provides an easy to use facade API for HttpClient based on the concept of a fluent interface. Fluent facade API exposes only the most fundamental functions of HttpClient and is indended for simple use cases that do not require the full flexibility of HttpClient. For instance, fluent facade API relieves the users from having to deal with connection management and resource deallocation

    // Execute a GET with timeout settings and return response content as String.
 Request.Get("http://somehost/")
        .connectTimeout(1000)
        .socketTimeout(1000)
        .execute().returnContent().asString();

Maven artifact.

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.2.5</version>
</dependency>

Solution 4

jsoup

jsoup is a library designed to parse HTML files. It does make HTTP calls to retrieve the source code of a web page.

Document doc = Jsoup.connect("http://en.wikipedia.org/").get();

Solution 5

Google HTTP Client

Another library is Google HTTP Client Library for Java.

Written by Google, this library is a flexible, efficient, and powerful Java client library for accessing any resource on the web via HTTP. It features a pluggable HTTP transport abstraction that allows any low-level library to be used, such as java.net.HttpURLConnection, Apache HTTP Client, or URL Fetch on Google App Engine. It also features efficient JSON and XML data models for parsing and serialization of HTTP response and request content. The JSON and XML libraries are also fully pluggable, including support for Jackson and Android's GSON libraries for JSON.

Share:
26,165
Dmitri
Author by

Dmitri

Updated on July 09, 2022

Comments

  • Dmitri
    Dmitri almost 2 years

    So, I've come to the conclusion that Apache HttpComponents 4 is one of the most overwrought APIs I've ever come across. Things that seem like they should be simple are taking hundreds of lines of code (and I'm still not sure resources get cleaned up correctly).

    Plus it wants me to do things like:

    List<NameValuePair> qparams = new ArrayList<NameValuePair>();
    qparams.add(new BasicNameValuePair("q", "httpclient"));
    qparams.add(new BasicNameValuePair("btnG", "Google Search"));
    qparams.add(new BasicNameValuePair("aq", "f"));
    qparams.add(new BasicNameValuePair("oq", null));
    URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", 
      URLEncodedUtils.format(qparams, "UTF-8"), null);
    

    Which, just... no. I know it's Java, and we're not into the whole brevity thing, but that's a little much. Not to mention the jars are up to 700KB.

    Anyway, enough ranting, I wanted to see what kind of experiences people have had with other HTTP client libraries?

    The ones I'm aware of are: Jetty, hotpotato, and AsyncHttpClient.

    This is for server-side use, I'm mostly interested in performance for many concurrent gets and large file transfers.

    Any recommendations?

    PS I know the venerable HttpClient 3.1 is still there, but I'd like to use something that's supported.

    Update

    @oleg: this is what the docs suggest:

        HttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet("http://www.apache.org/");
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    instream.read();
                } catch (IOException ex) {
                    throw ex;
                } catch (RuntimeException ex) {
                    httpget.abort();
                    throw ex;
                } finally {
                    try { instream.close(); } catch (Exception ignore) {}
                }
            }
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
    

    I still get unexpected errors when consuming entity content when using ThreadSafeClientConnManager. I'm sure it's my fault, but at this point I don't really want to have to figure it out.

    Hey, I don't mean to disparage anyone's work here, but I've been making a good-faith effort to use HttpComponents since 4.0 came out and it's just not working for me.