1.1 执行请求
HttpClient的最重要的功能是执行HTTP方法。一个HTTP方法的执行涉及到一个或多个HTTP请求或HTTP响应的交流,HttpClient通常是在内部处理的。用户将提供一个执行请求对象,HttpClient发送请求到目标服务器返回一个相应的响应对象,如果执行失败则抛出一个异常。所以,HttpClient API的主要切入点是HttpClient的接口,它定义了上述约定。
下面是一个请求执行过程中的最简单形式的例子:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
1.1.1 HTTP请求
所有的HTTP请求包含一个由请求行组成的一个方法名,一个请求的URI和一个HTTP协议的版本。
HttpClient的支持在HTTP/1.1规范中定义的所有的HTTP方法:GET, HEAD, POST, PUT, DELETE, TRACE
HttpGet httpget = new HttpGet(
HttpClient提供了一系列实用的方法来简化创建和修改请求URI。
URI可以组装编程:
URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
输出>
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
查询字符串也可以通过添加参数列表来生成:
List qparams = new ArrayList();
qparams.add(new BasicNameValuePair("q", "httpclient"));
qparams.add(new BasicNameValuePair("btnG", "Google Search"));
qparams.add(new BasicNameValuePair("aq", "f"));
qparams.add(new BasicNameValuePair("oq", null));
URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
HttpGet httpget = new HttpGet(uri);
System.out.println(httpget.getURI());
输出>
http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=
1.1.2 HTTP响应
HTTP响应是由服务器收到和解释一个请求消息后返回给客户端的消息。该消息的第一行包含遵循的协议版本,他由一个数字状态代码及其相关的文本来表示。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, "OK");
System.out.println(response.getProtocolVersion());
System.out.println(response.getStatusLine().getStatusCode());
System.out.println(response.getStatusLine().getReasonPhrase());
System.out.println(response.getStatusLine().toString());
输出>
HTTP/1.1
200
OK
HTTP/1.1 200 OK
1.1.3 Headers处理
一个HTTP消息可以包含一系列headers参数描述信息,例如内容长度,内容类型等。
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"");
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"
2
最有效获取所有的给定类型的headers方式是使用HeaderIterator接口。
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
response.addHeader("Set-Cookie",
response.addHeader("Set-Cookie",
HeaderIterator it = response.headerIterator("Set-Cookie");
while (it.hasNext()) {
}
输出>
Set-Cookie: c1=a; path=/; domain=localhost
Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
它还提供了方便的方法来解析HTTP消息的单个header元素
HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
response.addHeader("Set-Cookie",
response.addHeader("Set-Cookie",
HeaderElementIterator it = new BasicHeaderElementIterat
while (it.hasNext()) {
输出>
c1 = a
path=/
domain=localhost
c2 = b
path=/
c3 = c
domain=localhost
1.1.4 HTTP 实体
HTTP消息的可以进行内容实体和请求或响应关联。可以在一些要求和一些回应中找到实体,因为它们是可选的。实体内附与请求之中。
HttpClient的区分三种不同实体的地方在于内容来源于:
streamed流媒体:内容是从收到的流,或在运行中产生的。特别是包括被从HTTP响应收到的实体。流媒体的实体一般不可重复。
self-contained独立的:内容是在内存或以从一个连接或其他实体的独立获得的。独立的的实体,一般可重复的。这种类型的实体将主要用于内附在HTTP请求中。
wrapping包装:实体内容是从另一个实体获得。
当得到一个HTTP响应流的内容的时候,这种区分对于连接管理是很重要的。对于请求实体,通过应用程序来创建和只通过使用的HttpClient发送,流和自载之间差别很小。在这种情况下,建议考虑非重复的流实体,以及那些重复的自载实体。
1.1.4.1 重复实体
一个实体可以是可重复的,这意味着它的内容可以被读取一次以上。唯一有可能是的独立的实体(如ByteArrayEntity或StringEntity())
1.1.4.2 使用HTTP实体
由于一个实体能够表示二进制和字符的内容,它可以提供编码的支持(支持文字、IE和字符内容)。
这个实体在执行封闭内容的请求的时候或者在请求成功和响应返回成功的时候被创建。
若要读取从实体内容,一可以通过检索HttpEntity#getContent()方法,它返回一个java.io.InputStream,或一个可以提供一个输出流的HttpEntity#writeTo(OutputStream中)方法的输入流,这将返回已被写入给定的流的所有内容。
当通过传入的消息收到实体,方法HttpEntity#getContentType()和HttpEntity#getContentLength()方法可用于阅读通用元数据metadata,如Content-Type,Content-Length headers(如果可用)。由于Content-Type header可以包含一个像text/plain或者text/html的文本mime-types的character encoding,HttpEntity#getContentEncoding()方法用来读取此信息。如果headers是不可用,返回的长度是-1,content type并为NUL。如果Content – Type header可用,将返回一个header对象。
当创建了一个即将卸任的消息实体,该meta data必须提供由该实体的创造者。
StringEntity myEntity = new StringEntity("important message",
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);
stdout >
Content-Type: text/plain; charset=UTF-8
17
UTF-8
important message
17
1.1.5 确保资源释放
当响应实体完成之后,重要的是要确保所有的实体内容已被完全消耗,使该连接可以安全地返回到连接池,重新由连接管理器提供给后续请求使用。最简单的方法是调用HttpEntity#consumeContent()方法来消耗流上的所有可用的内容。当检测到内容已经达到流末尾的时候,HttpClient会自动释放底层连接返回到连接管理器。HttpEntity#consumeContent()方法多次调用也是安全的。
当只有小部分实体响应内容需要被检索和消费。其余内容,使用可重复的连接性能损失太大,可以简单地调用HttpUriRequest#abort()方法来终止请求。
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
该连接将不可重用,但是所有资源会被释放。
1.1.6 获取实体内容
获取实体内容推荐的方法是通过使用HttpEntity#getContent()或HttpEntity#writeTo(OutputStream中)方法。
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
在某些情况下,实体内容可能需要能够读被多次读取。在这种情况下实体的内容必须以某种方式被缓冲在内存或磁盘上。最简单的方法是通过BufferedHttpEntity类来封装原始实体。原始实体内容可以从内存中的缓冲区来读取。其他方式封装实体都包含原始实体。
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
}
1.1.7 生产实体内容
HttpClient提供了一些类可以从HTTP连接内容中获得效地流。这些类的实例可以与实体内附如POST和PUT请求,以便为即将离任的HTTP请求附上实体内容。
File file = new File("somefile.txt");
FileEntity entity = new FileEntity(file, "text/plain; charset="UTF-8"");
HttpPost httppost = new HttpPost("http://localhost/action.do");
httppost.setEntity(entity);
请注意InputStreamEntity是不可重复的,因为它只能从底层数据流中读取一次。一般来说,建议实现自定义HttpEntity类是自载的,而不是使用通用InputStreamEntity。
1.1.7.1 动态内容实体
通常的HTTP实体需要在执行上下文的时候动态生成的。
ContentProducer cp = new ContentProducer() {
};
HttpEntity entity = new EntityTemplate(cp);
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
1.1.7.2 HTML forms
许多应用程序经常需要模拟一个HTML表单提交的过程,例如,以登录到Web应用程序或提交的输入数据。
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("param1", "value1"));
formparams.add(new BasicNameValuePair("param2", "value2"));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
HttpPost httppost = new HttpPost("http://localhost/handler.do");
httppost.setEntity(entity);
这UrlEncodedFormEntity实例将使用
param1=value1¶m2=value2
1.1.7.3 内容组块
一般来说,建议当HTTP消息的发送的时候让HttpClient的选择最合适的传输编码。这是可能的,但是,HttpClient首选通过设置HttpEntity#setChunked()为true设置编码。请注意的HttpClient将使用这个标志作为提示。当使用HTTP协议的版本不支持时候该值将被忽略时,如HTTP/1.0的块编码。
StringEntity entity = new StringEntity("important message",
entity.setChunked(true);
HttpPost httppost = new HttpPost("http://localhost/acrtion.do");
httppost.setEntity(entity);
1.1.8 响应处理程序
最简单和最方便的处理响应方式是通过使用ResponseHandler接口。这种方法完全免除了用户去担心连接管理。无论执行请求是否成功或导致异常,HttpClient的ResponseHandler会确保自动释放该连接回连接管理器。
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost/");
ResponseHandler handler = new ResponseHandler() {
};
byte[] response = httpclient.execute(httpget, handler);