设为首页 加入收藏

TOP

Httpclient4.4之原理(请求执行)(二)
2015-07-16 12:56:20 来源: 作者: 【 】 浏览:40
Tags:Httpclient4.4 原理 请求 执行
ator("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消息并获得header中一个个独立的header元素,示例:


package httpclienttest;
?
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.Nameva luePair;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.message.BasicHttpResponse;
?
public class T5 {
? ? public static void main(String[] args) {
? ? ? ? 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
?


4. HTTP Entity


HTTP消息能携带与请求或响应有关的实体内容,它只是可选的,能在有些请求或响应中找到。请求消息中使用实体是针对entity enclosing request的,HTTP规范中定义了两个entity enclosing request方法:POST和PUT。响应通常都是包装一个实体的,当然也有例外!


HttpClient区分三种实体,根据其内容来源:


?streamed(流):内容从流中接收的。流实体是不可重复的。



?self-contained(自包含):内容在内存中或者从连接或其它实体自主获得的。自包含的实体通常都是可重复的。这种类型的实体通常用于entity enclosing request。



?wrapping(包装):内容从其它实体获得。



从HTTP响应中流出内容时,对于连接管理(connection management)这种区别还是很重要的。对于请求实体是通过应用程序创建的并且只是使用HttpClient发送,这种区别对于streamed与self-contained意义不大。这种情况下,建议把不可重复的实体当作streamed,把那些可重复的当作self-contained。


4.1 可重复的实体


一个实体能被重复获取,意味着内容能被读多次。这是唯一可能的自包含实体(如:ByteArrayEntity或StringEntity)。


4.2 使用HTTP实体


因为实体可以代表二进制和字符内容,它是支持字符集的(支持后者,即文字内容)。


从实体中读取内容,可能通过HttpEntity的getContent()方法检索输入流,它返回一个java.io.InputStream,或者可以提供一个输出流作为HttpEntity的writeTo(OutputStream)方法的参数,将返回的所有内容写入给定的流。


当实体收到一个进来的消息时,HttpEntity的getContentType方法和getContentLength方法能被用来读取请求header里的元数据:Content-Type和Content-Length(如果它们可用)。由于header的Content-Type能包含像text/plain或text/html这样的MIME类型的字符集(编码),HttpEntity的getContentEncoding()方法用来读取这个信息。 如果header不可用,长度将返回-1和内容类型为NULL。如果header的Content-Type是可用的,这个Header对象将返回。


创建一个输出消息的实体时,该数据是由实体的创建者提供,示例:
?
package httpclienttest;
?
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.util.EntityUtils;
?
public class T6 {
? ? public static void main(String[] args) throws Exception{
? ? ? ? StringEntity myEntity = new StringEntity("important message",
? ? ? ? ? ? ? ? ContentType.create("text/plain","UTF-8"));
? ? ? ? System.out.println(myEntity.getContentType());
? ? ? ? System.out.println(myEntity.getContentLength());
? ? ? ? System.out.println(EntityUtils.toString(myEntity));
? ? ? ? System.out.println(EntityUtils.toByteArray(myEntity).length);
? ? }
}
?


输出为:
?
Content-Type: text/plain; charset=UTF-8
17
important message
17
?


5. 确保底层资源

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

评论

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