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.
@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.
Lighttpd, F5 NGINX, LiteSpeed Web Server, and Plesk are the most popular alternatives and competitors to Apache Server for reviewers from smaller sized companies.
Apache HttpClient The Apache HTTP client was the first widely adopted open source client to be released, arriving in its original form in 2002. Now on its 5th major version, it's probably still the most commonly used client outside of Java's core libraries.
Is there any way to use it in java 8? No, because the jdk. incubator. http module has been added since Java 9.
RestTemplate delegates to a ClientHttpRequestFactory, and one of the implementations of this interface uses Apache's HttpClient.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With