URLConnection getInputStream() 和 URLConnection getInputStream() 之间的区别HttpEntity 获取内容()

发布于 2024-11-30 17:26:51 字数 1502 浏览 1 评论 0原文

我尝试从特定网址下载图片,首先我使用这种方式获取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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-12-07 17:26:51

看起来服务器返回图像内容,但也返回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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文