Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

请求获取网页的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();
           }
       }
   }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...