Alternatives to Apache HttpComponents?

Dmitri picture Dmitri · Mar 24, 2011 · Viewed 25k times · Source

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.

Answer

ok2c picture ok2c · Mar 24, 2011

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.