URLConnection getInputStream() 和 URLConnection getInputStream() 之间的区别HttpEntity 获取内容()
我尝试从特定网址下载图片,首先我使用这种方式获取InputStream:
if (url != null) {
URLConnection ucon = null;
try {
ucon = url.openConnection();
} catch (IOException e2) {
e2.printStackTrace();
}
if (ucon != null) {
ucon.setConnectTimeout(CONN_TIMEOUT);
ucon.setReadTimeout(READ_TIMEOUT);
try {
is = ucon.getInputStream();
效果很好,但是当我尝试从 http://111.12.12.232/images/face/bigface/339.gif 我无法获取InputStream,但是尝试使用:
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();
这样可以成功获取InputStream,并且可以下载gif。
所以我想知道这两种方法有什么不同? 谢谢~
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();
}
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~
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来服务器返回图像内容,但也返回404 响应代码,表示执行请求时发生错误。
在 1.6 Sun/Oracle JDK 上,当 HttpURLConnection 注意到一个 IOException 时,它似乎会失败并出现 IOException像这样返回代码,并且不尝试返回内容。我的猜测是,Android 平台具有相同的行为,并且您使用的 Apache HttpClient 库对于服务器错误配置更加稳健。
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.