Difference between URLConnection getInputStream() & HttpEntity getContent()
I try to download pic from the specific url, firstly I use this way to get InputStream:
if (url != null) {
URLConnection ucon = null;
try {
ucon = url.openConnection();
} catch (IOException e2) {
e2.printStackTrace();
开发者_C百科 }
if (ucon != null) {
ucon.setConnectTimeout(CONN_TIMEOUT);
ucon.setReadTimeout(READ_TIMEOUT);
try {
is = ucon.getInputStream();
It works good, but when I try to download pic from http://111.12.12.232/images/face/bigface/339.gif I can't get the InputStream, but try to use :
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, false);
HttpConnectionParams.setConnectionTimeout(params, CONN_TIMEOUT);
HttpConnectionParams.setSoTimeout(params, READ_TIMEOUT);
HttpGet getRequest;
try {
getRequest = new HttpGet(url.toURI());
HttpClient client = new DefaultHttpClient(params);
HttpResponse response = client.execute(getRequest);
HttpEntity entity = response.getEntity();
is = entity.getContent();
This way can get InputStream successfully, and can download the gif.
So I wonder what's the different between the two methods? Thanks~
It looks like the server returns the image content but also returns a 404 response code, which indicates an error fulfilling the request.
On the 1.6 Sun/Oracle JDK, the HttpURLConnection seems to fail with an IOException when it notices a return code like this, and does not attempt to return content. My guess is that the Android platform has this same behavior, and the Apache HttpClient library you used is a bit more robust to server misconfigurations.
精彩评论