设为首页 加入收藏

TOP

Httpclient4.4之原理(请求执行)(五)
2015-07-16 12:56:20 来源: 作者: 【 】 浏览:38
Tags:Httpclient4.4 原理 请求 执行
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 sm = reader.lines();
? ? ? ? ? ? ? ? ? ? sm.forEach(System.out::println);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? System.out.println("读第二次!");
? ? ? ? ? ? ? ? try(BufferedReader reader = new BufferedReader(new InputStreamReader(
? ? ? ? ? ? ? ? ? ? ? ? entity.getContent(),StandardCharsets.UTF_8))){
? ? ? ? ? ? ? ? ? ? Stream sm = reader.lines();
? ? ? ? ? ? ? ? ? ? sm.forEach(System.out::println);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }catch(Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}


7. 创建实体内容


HttpClient提供了几个通过HTTP connection有效地流出内容的类。这些类的实例与POST和PUT这样的entity enclosing requests有关,为了把这些实体内容放进即将发出的请求中。HttpClient提供的这几个类大多数都是数据容器,像字符串,字节数组,输入流和文件对应的:StringEntity,ByteArrayEntity,InputStreamEntity和FileEntity。示例:


File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file, ContentType.create("text/plain", "UTF-8"));
HttpPost httppost = new HttpPost("http://localhost/action.do");
httppost.setEntity(entity);
?


请注意:InputStreamEntity是不可重复的,因为它只能从底层数据流读一次。通常推荐使用InputStreamEntity来实现一个自定义的self-contained的HttpEntity类。


7.1 HTML表单


许多应用程序需要模拟提交一个HTML表单的过程,例如,为了登录一个web应用程序或者提交输入数据,HttpClient提供一个实体类UrlEncodedFormEntity来帮助完成这个过程。


List formparams = new ArrayList();
formparams.add(new BasicNameva luePair("param1", "value1"));
formparams.add(new BasicNameva luePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
?


这UrlEncodedFormEntity实体将使用URL编码来编码参数并产生如下内容:


param1=value1¶m2=value2
?


7.2 HTTP分块


通常推荐HttpClient选择适当的基于HTTP消息传输特性的传输编码。然而,有个可能是通知HttpEntity优先使用分块编码(chunk coding),通过HttpEntity的setChunked()方法设置为true。请注意,HttpClient使用此标记仅仅是为了提示。当使用HTTP协议不支持分块编码时,这个值将被忽略,如:HTTP/1.0。示例:
?
StringEntity entity = new StringEntity("important message",
? ? Co

首页 上一页 2 3 4 5 下一页 尾页 5/5/5
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Java8之lambda表达式(变量作用域.. 下一篇详解Linux平台芯片烧写流程

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: