设为首页 加入收藏

TOP

HttpClient 教程(二)
2015-07-16 12:55:54 来源: 作者: 【 】 浏览:27
Tags:HttpClient 教程
der(“Set-Cookie”,
“c2=b; path=\”/\”, c3=c; domain=\”localhost\””);
Header h1 = response.getFirstHeader(“Set-Cookie”);
System.out.println(h1);
Header h2 = response.getLastHeader(“Set-Cookie”);
System.out.println(h2);
Header[] hs = response.getHeaders(“Set-Cookie”);
System.out.println(hs.length);
输出内容为:


Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path=”/”, c3=c; domain=”localhost”
获得给定类型的所有头部信息最有效的方式是使用HeaderIterator接口。


HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, “OK”);
response.addHeader(“Set-Cookie”,
“c1=a; path=/; domain=localhost”);
response.addHeader(“Set-Cookie”,
“c2=b; path=\”/\”, c3=c; domain=\”localhost\””);
HeaderIterator it = response.headerIterator(“Set-Cookie”);
while (it.hasNext()) {
System.out.println(it.next());
}
输出内容为:


Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path=”/”, c3=c; domain=”localhost”
它也提供解析HTTP报文到独立头部信息元素的方法方法。


HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, “OK”);
response.addHeader(“Set-Cookie”,
“c1=a; path=/; domain=localhost”);
response.addHeader(“Set-Cookie”,
“c2=b; path=\”/\”, c3=c; domain=\”localhost\””);
HeaderElementIterator it = new BasicHeaderElementIterator(
response.headerIterator(“Set-Cookie”));
while (it.hasNext()) {
HeaderElement elem = it.nextElement();
System.out.println(elem.getName() + ” = ” + elem.getValue());
Nameva luePair[] params = elem.getParameters();
for (int i = 0; i < params.length; i++) {
System.out.println(” ” + params[i]);
}
}
输出内容为:


c1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = c
domain=localhost
1.1.4 HTTP实体
HTTP报文可以携带和请求或响应相关的内容实体。实体可以在一些请求和响应中找到,因为它们也是可选的。使用了实体的请求被称为封闭实体请求。HTTP规范定义了两种封闭实体的方法:POST和PUT。响应通常期望包含一个内容实体。这个规则也有特例,比如HEAD方法的响应和204 No Content,304 Not Modified和205 Reset Content响应。


HttpClient根据其内容出自何处区分三种类型的实体:


streamed流式:内容从流中获得,或者在运行中产生。特别是这种分类包含从HTTP响应中获取的实体。流式实体是不可重复生成的。
self-contained自我包含式:内容在内存中或通过独立的连接或其它实体中获得。自我包含式的实体是可以重复生成的。这种类型的实体会经常用于封闭HTTP请求的实体。
wrapping包装式:内容从另外一个实体中获得。
当从一个HTTP响应中获取流式内容时,这个区别对于连接管理很重要。对于由应用程序创建而且只使用HttpClient发送的请求实体,流式和自我包含式的不同就不那么重要了。这种情况下,建议考虑如流式这种不能重复的实体,和可以重复的自我包含式实体。


1.1.4.1 重复实体
实体可以重复,意味着它的内容可以被多次读取。这就仅仅是自我包含式的实体了(像ByteArrayEntity或StringEntity)。


1.1.4.2 使用HTTP实体
因为一个实体既可以代表二进制内容又可以代表字符内容,它也支持字符编码(支持后者也就是字符内容)。


实体是当使用封闭内容执行请求,或当请求已经成功执行,或当响应体结果发功到客户端时创建的。


要从实体中读取内容,可以通过HttpEntity#getContent()方法从输入流中获取,这会返回一个java.io.InputStream对象,或者提供一个输出流到HttpEntity#writeTo(OutputStream)方法中,这会一次返回所有写入到给定流中的内容。


当实体通过一个收到的报文获取时,HttpEntity#getContentType()方法和HttpEntity#getContentLength()方法可以用来读取通用的元数据,如Content-Type和Content-Length头部信息(如果它们是可用的)。因为头部信息Content-Type可以包含对文本MIME类型的字符编码,比如text/plain或text/html,HttpEntity#getContentEncoding()方法用来读取这个信息。如果头部信息不可用,那么就返回长度-1,而对于内容类型返回NULL。如果头部信息Content-Type是可用的,那么就会返回一个Header对象。


当为一个传出报文创建实体时,这个元数据不得不通过实体创建器来提供。


StringEntity myEntity = new StringEntity(“important message”,
“UTF-8″);
System.out.println(myEntity.getContentType());
System.out.println(myEntity.getContentLength());
System.out.println(EntityUtils.getContentCharSet(myEntity));
System.out.println(EntityUtils.toString(myEntity));
System.out.println(EntityUtils.toByteArray(myEntity).length);
输出内??为


Content-Type: text/plain; charset=UTF-8
17
U

首页 上一页 1 2 3 4 5 6 下一页 尾页 2/6/6
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Spring REST 异常处理 下一篇使用HttpClient4来构建Spring Res..

评论

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