Httpclient4.4之原理(请求执行)(4)

消费实体内容的推荐方式是使用HttpEntity的getContent()或HttpEntity的writeTo(OutputStream)方法。HttpClient还配备了EntityUtils类,它提供了几个静态方法让读取实体内容与信息更容易,而不是直接读java.io.InputStream。能通过EntityUtils的这些静态方法检索整个内容体到这符串/字节数组。不管怎样,强烈建议不要使用EntityUtils,除非响应实体产生自一个可信的HTTP服务端并且知道是有限长度的。示例:
 
package httpclienttest;
 
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
 
public class T9 {
    public static void main(String[] args) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://www.ifeng.com/");
        try (CloseableHttpResponse response = httpclient.execute(httpget)){
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                System.out.println(EntityUtils.toString(entity, StandardCharsets.UTF_8));
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
 

在某些情况下,可能需要读实体内容不止一次。这种情况下,实体内容在某种程度上必须缓冲,无论在内存还是磁盘。最简单的方法是通过BufferedHttpEntity类来包装原始的实体,这将使原始实体的内容读到内存缓冲区。示例:
 
package httpclienttest;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class T10 {
    public static void main(String[] args) {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet httpget = new HttpGet("http://www.ifeng.com/");
        try (CloseableHttpResponse response = httpclient.execute(httpget)){
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                //实体进行缓冲,可重复使用
                entity = new BufferedHttpEntity(entity);
                try(BufferedReader reader = new BufferedReader(new InputStreamReader(
                        entity.getContent(),StandardCharsets.UTF_8))){
                    Stream<String> sm = reader.lines();
                    sm.forEach(System.out::println);
                }
                System.out.println("读第二次!");
                try(BufferedReader reader = new BufferedReader(new InputStreamReader(
                        entity.getContent(),StandardCharsets.UTF_8))){
                    Stream<String> sm = reader.lines();
                    sm.forEach(System.out::println);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}

7. 创建实体内容

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/9e6df74f30b5635d9e9708bc9e48a617.html