请求获取网页的response,获取网页的html 怎么那么慢

image.png

HttpEntity multipart = builder.build();
            httppost.setEntity(multipart);
            long start = System.currentTimeMillis();
            // 发送请求
            response = httpclient.execute(httppost);
            long end = System.currentTimeMillis();
            System.out.println("查询upsfreight消耗的时间是(毫秒):" + (end - start));
            start = System.currentTimeMillis();
            HttpEntity entity = response.getEntity();
            String html = EntityUtils.toString(entity, "utf-8");
            end = System.currentTimeMillis();
            System.out.println("转换时间:" + (end - start));

String html = EntityUtils.toString(entity, "utf-8");
这一行代码要执行很久 4-9s,我有什么办法把他加快吗

下面是EntityUtils.toString()的方法,包为
image.png

public static String toString(HttpEntity entity, Charset defaultCharset) throws IOException, ParseException {
       Args.notNull(entity, "Entity");
       InputStream instream = entity.getContent();
       if (instream == null) {
           return null;
       } else {
           try {
               Args.check(entity.getContentLength() <= 2147483647L, "HTTP entity too large to be buffered in memory");
               int i = (int)entity.getContentLength();
               if (i < 0) {
                   i = 4096;
               }

               Charset charset = null;

               try {
                   ContentType contentType = ContentType.get(entity);
                   if (contentType != null) {
                       charset = contentType.getCharset();
                   }
               } catch (UnsupportedCharsetException var13) {
                   if (defaultCharset == null) {
                       throw new UnsupportedEncodingException(var13.getMessage());
                   }
               }

               if (charset == null) {
                   charset = defaultCharset;
               }

               if (charset == null) {
                   charset = HTTP.DEF_CONTENT_CHARSET;
               }

               Reader reader = new InputStreamReader(instream, charset);
               CharArrayBuffer buffer = new CharArrayBuffer(i);
               char[] tmp = new char[1024];

               int l;
               while((l = reader.read(tmp)) != -1) {
                   buffer.append(tmp, 0, l);
               }

               String var9 = buffer.toString();
               return var9;
           } finally {
               instream.close();
           }
       }
   }
阅读 2.6k
2 个回答

所以EntityUtils的这个方法的逻辑是什么呢。。。

这个地方不转UTF8不行吗?默认不是UTF8吗

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏