最近使用HttpClient4.3开源包,发现了EntityUtils这个对象,EntityUtils对象是org.apache.http.util下的一个工具类,用官方的解释是为HttpEntity对象提供的静态帮助类,其常用的几个方法如下:
consume()方法;
consumeQuietly(HttpEntity)方法
toByteArray(final HttpEntity entity)方法
最主要的就是consume()这个方法,其功能就是关闭HttpEntity是的流,如果手动关闭了InputStream instream = entity.getContent();这个流,也可以不调用这个方法。看看了其源码就知道了:
public final class EntityUtils { private EntityUtils() { } public static void consumeQuietly(final HttpEntity entity) { try { consume(entity); } catch (final IOException ignore) { } } public static void consume(final HttpEntity entity) throws IOException { if (entity == null) { return; } if (entity.isStreaming()) { final InputStream instream = entity.getContent(); if (instream != null) { instream.close(); } } } public static void updateEntity( final HttpResponse response, final HttpEntity entity) throws IOException { Args.notNull(response, "Response"); consume(response.getEntity()); response.setEntity(entity); } public static byte[] toByteArray(final HttpEntity entity) throws IOException { Args.notNull(entity, "Entity"); final InputStream instream = entity.getContent(); if (instream == null) { return null; } try { Args.check(entity.getContentLength() <= Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory"); int i = (int)entity.getContentLength(); if (i < 0) { i = 4096; } final ByteArrayBuffer buffer = new ByteArrayBuffer(i); final byte[] tmp = new byte[4096]; int l; while((l = instream.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toByteArray(); } finally { instream.close(); } } @Deprecated public static String getContentCharSet(final HttpEntity entity) throws ParseException { Args.notNull(entity, "Entity"); String charset = null; if (entity.getContentType() != null) { final HeaderElement values[] = entity.getContentType().getElements(); if (values.length > 0) { final NameValuePair param = values[0].getParameterByName("charset"); if (param != null) { charset = param.getValue(); } } } return charset; } @Deprecated public static String getContentMimeType(final HttpEntity entity) throws ParseException { Args.notNull(entity, "Entity"); String mimeType = null; if (entity.getContentType() != null) { final HeaderElement values[] = entity.getContentType().getElements(); if (values.length > 0) { mimeType = values[0].getName(); } } return mimeType; } public static String toString( final HttpEntity entity, final Charset defaultCharset) throws IOException, ParseException { Args.notNull(entity, "Entity"); final InputStream instream = entity.getContent(); if (instream == null) { return null; } try { Args.check(entity.getContentLength() <= Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory"); int i = (int)entity.getContentLength(); if (i < 0) { i = 4096; } Charset charset = null; try { final ContentType contentType = ContentType.get(entity); if (contentType != null) { charset = contentType.getCharset(); } } catch (final UnsupportedCharsetException ex) { throw new UnsupportedEncodingException(ex.getMessage()); } if (charset == null) { charset = defaultCharset; } if (charset == null) { charset = HTTP.DEF_CONTENT_CHARSET; } final Reader reader = new InputStreamReader(instream, charset); final CharArrayBuffer buffer = new CharArrayBuffer(i); final char[] tmp = new char[1024]; int l; while((l = reader.read(tmp)) != -1) { buffer.append(tmp, 0, l); } return buffer.toString(); } finally { instream.close(); } } public static String toString( final HttpEntity entity, final String defaultCharset) throws IOException, ParseException { return toString(entity, defaultCharset != null ? Charset.forName(defaultCharset) : null); } public static String toString(final HttpEntity entity) throws IOException, ParseException { return toString(entity, (Charset)null); } }
相关推荐
httpclient4.3的二次封装,解决了代理问题、多线程问题、SSL问题、通用的get、post请求
httpclient4.3开发的工具类,httpclientUtils 因为项目需要,自己就根据常用的写了一个。
httpclient4.3所需jar包
httpClient4.3官方包,官方源文件,可以直接使用
这个压缩包包含了HttpClient4.3中文版pdf和英文版pdf 。word文档里也有几个学习的网址。如果用HttpClient4.5的话上面的知识基本(95%)能用的上。省去了到处找零碎资料的时间。
压缩包内包括 httpclient 4.3的jar包和完整的demo程序。想学httpclient 4的赶紧下啊。
httpclient4.3.x和其依赖jar包。
httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4httpclient4.3 和 httpcore4.4
httpClient4.3的jar包
httpclient中文版本,HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的...本文首先介绍 HTTPClient,然后根据作者实际工作经验给出了一些常见问题的解决方法.
提供HttpClient4.3所需要的jar
HTTPclient 4.3版本的Apache官网英文版的翻译版本
javase http通讯技术 apache httpclient4.3 设置代理详解
NULL 博文链接:https://chun521521.iteye.com/blog/2422270
Apache Httpclient4.3.x 系列jar套件
HttpClient4.3教程共51页.pdf.zip
最新版httpclient自动登录人人代码,内含有代码,httpclient4.3包,只需要修改账户和密码,就可以登陆
HttpClient 4.3教程.rar
httpclient4.3 封装工具类.zip
网上收集整理的HttpClient4.3教程已转成PDF格式。