Apache HTTP client or URLConnection
For most things I'd say that HttpClient
is the way to go. However there are some situations and edge cases where I'd fall back to a URLConnection
. Examples of edge cases here and here
EDIT
A similar question has been asked before: httpclient vs httpurlconnection. I would assume that I would find HttpUrlConnection
is somewhat faster as the HttpClient
is built on top of the standard Java libraries. HoweverHttpClient
code much quicker and easier to write and maintain. According to a comments below, the core elements of HttpClient
have been performance optimised.
If performance is a major concern your best bet is to write two clients, one using each method, then benchmark them both. If you do this, please let us know the results.
URLConnection or HTTPClient: Which offers better functionality and more efficiency?
I believe in this case it's up to whichever API you find more natural. Generally, HTTPClient is more efficient inside a server side application (or maybe batch application), because it allows you to specify a multithreaded connection pool, with a max number of total connections, and a max per host connection count (which ensures concurrent connections to the same host don't get serialized (a problem with HttpUrlConnection)). But in an android app, you'll probably only be making a single connection at a time, so this doesn't matter.
httpURLConnection vs apache commons http
The things drive me to Apache HttpClient are,
- Buggy keep-alive support.
- 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.
How to switch from HttpClient to HttpUrlConnection?
You should absolutely be using HttpUrlConnection
:
For Gingerbread and better, HttpURLConnection is the best choice... New applications should use HttpURLConnection...
--Google (circa. 2011)
However, there is no easy way just to "switch". The APIs are totally different. You are going to have to rewrite your networking code. There are perfect examples in the documentation on how to submit a GET and POST requests as well as in the SDK sample apps.
Connection to a URL from within an applet using Apache's HttpClient vs using the JDK's URLConnection
This is a common problem with libraries implementing their own URL connection via Socket. Apparently, the JRE implementation of the URLConnection class can get to the browser information directly. We had to employ the technique as mentioned by oscargm above, i.e. on the appserver writing the request cookies to be the parameters to the applet AND getting to the browser's document cookies using JavaScript (this is for the case of SSO where the set of cookies may not be the same because of the intermediate agent -- proxy servers). Note that if the cookies are HttpOnly -- the javascript code will fail.
Related Topics
Spannable on Android for Textview
How to Launch Home Screen Programmatically in Android
How to Crop a Bitmap for Imageview
No Resource Identifier Found for Attribute 'Layout_Behavior' in Package
Is There an Advantage to Running Jruby If You Don't Know Any Java
Integration Testing Frameworks for Testing a Distributed System
Translating Ruby Encryption Code to Java
How to Run an R Program from Java
Install Xlsx and Rjava on MACos Mavericks 10.9.5
Simple Program to Call R from Java Using Eclipse and Rserve
Java Object Analogue to R Data.Frame
Version Control for One-Man Project Using Eclipse
Android - Imageloader Must Be Init with Configuration Before Using in Uil
Manifest Merger Failed:Uses-Sdk:Minsdkversion 8 Cannot Be Smaller