zoukankan      html  css  js  c++  java
  • HttpClient之EntityUtils工具类

     今天看到tttpclient-tutorial上面有这样一句话-----非常的不推荐使用EntityUtils,除非知道Entity是来自可信任的Http Server 而且还需要知道它的最大长度。文档上面给了如下示例:

    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpGet httpget = new HttpGet("http://localhost/");
    CloseableHttpResponse response = httpclient.execute(httpget);
    try {
        HttpEntity entity = response.getEntity();
        if (entity != null) {
            long len = entity.getContentLength();
            if (len != -1 && len < 2048) {
                System.out.println(EntityUtils.toString(entity));
            } else {
                // Stream content out
            }
        }
    } finally {
        response.close();
    }

    看这意思,contentLength < 2048,你就直接干吧, 否则你还是自已拿着stream玩吧!

    无论看了下EntityUtils.toString(entity)方法的源码,现记录一下,以便于复习

    private static String toString(
                final HttpEntity entity,
                final ContentType contentType) throws IOException {
            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 capacity = (int)entity.getContentLength();
                if (capacity < 0) {
                    capacity = DEFAULT_BUFFER_SIZE;
                }
                Charset charset = null;
                if (contentType != null) {
                    charset = contentType.getCharset();
                    if (charset == null) {
                        final ContentType defaultContentType = ContentType.getByMimeType(contentType.getMimeType());
                        charset = defaultContentType != null ? defaultContentType.getCharset() : null;
                    }
                }
                if (charset == null) {
                    charset = HTTP.DEF_CONTENT_CHARSET;
                }
                final Reader reader = new InputStreamReader(inStream, charset);
                final CharArrayBuffer buffer = new CharArrayBuffer(capacity);
                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();
            }
        }

    需要注意以下两点:

      (1)Args.check(entity.getContentLength() <= Integer.MAX_VALUE, "HTTP entity too large to be buffered in memory"); 其实就是对Entity的大小做了一次检测,

    如果太大推荐就放到内存中,只是这里的阈值是Integer.MAX_VALUE。 

      (2)将InputStream还原成String是时,使用了CharArrayBuffer 类,以前都没用过了呢, 以后也可以尝试着用用!

      (3)关流!

  • 相关阅读:
    关于'selffilter' is not a registered tag library. Must be one of:
    关于Django报错django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configure
    大杂烩
    PyQt5多个GUI界面设计
    OpenCV读取图像问题:OpenCV(3.4.3) D:BuildOpenCVopencv-size.width0 && size.height0 in function 'cvimshow'
    MOOC课程信息D3.js动态可视化
    详解教务系统模拟登录与爬取二
    教务系统模拟登录与成绩爬取一
    pyecharts各省人口GDP可视化分析
    python爬虫伪装技术应用
  • 原文地址:https://www.cnblogs.com/z-qinfeng/p/11706203.html
Copyright © 2011-2022 走看看