Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

httpURLConnection vs apache commons http

I just wanted to know if any of you had any problems using java default HttpURLConnection class. Some kind of bug that made you switch to apache commons.

Or is it just the (ugly) interface that class exposes that justifies the birth of 3rd party http lib?

Disclosure: I heard some arguments against java.net having some serious problems, but I'm finding hard to believe that a class that is part of the java core distribution still has issues after several releases of the JDK

like image 835
Pablo Fernandez Avatar asked May 17 '10 19:05

Pablo Fernandez


2 Answers

The things drive me to Apache HttpClient are,

  1. Buggy keep-alive support.
  2. Cookie handling.

You should use HttpClient 4 (Apache HTTP Components) now.

EDIT: The first problem has been discussed several times here. See,

HttpURLConnection.getResponseCode() returns -1 on second invocation

HttpURLConnection: What's the deal with having to read the whole response?

Even though the problem seems to be worse on Android, we saw the exact problems on J2SE.

like image 97
ZZ Coder Avatar answered Sep 28 '22 11:09

ZZ Coder


The Android SDK says Prefer HttpURLConnection for new code.

Android includes two HTTP clients: HttpURLConnection and Apache HTTP Client. Both support HTTPS, streaming uploads and downloads, configurable timeouts, IPv6 and connection pooling. Apache HTTP client has fewer bugs in Android 2.2 (Froyo) and earlier releases. For Android 2.3 (Gingerbread) and later, HttpURLConnection is the best choice. Its simple API and small size makes it great fit for Android. Transparent compression and response caching reduce network use, improve speed and save battery. See the Android Developers Blog for a comparison of the two HTTP clients.

like image 30
Edward Brey Avatar answered Sep 28 '22 11:09

Edward Brey