HttpClient 4.3教程(转载)

转自:http://www.yeetrack.com/?p=779

前言

Http协议应该是互联网中最重要的协议。持续增长的web服务、可联网的家用电器等都在继承并拓展着Http协议,向着浏览器之外的方向发展。

虽然jdk中的java.net包中提供了一些基本的方法,通过http协议来访问网络资源,但是大多数场景下,它都不够灵活和强大。HttpClient致力于填补这个空白,它可以提供有效的、最新的、功能丰富的包来实现http客户端。

为了拓展,HttpClient即支持基本的http协议,还支持http-aware客户端程序,如web浏览器,Webservice客户端,以及利用or拓展http协议的分布式系统。

1、HttpClient的范围/特性

  • 是一个基于HttpCore的客户端Http传输类库
  • 基于传统的(阻塞)IO
  • 内容无关

2、HttpClient不能做的事情

  • HttpClient不是浏览器,它是一个客户端http协议传输类库。HttpClient被用来发送和接受Http消息。HttpClient不会处理http消息的内容,不会进行javascript解析,不会关心content type,如果没有明确设置,httpclient也不会对请求进行格式化、重定向url,或者其他任何和http消息传输相关的功能。

第一章 基本概念

1.1. 请求执行

HttpClient最基本的功能就是执行Http方法。一个Http方法的执行涉及到一个或者多个Http请求/Http响应的交互,通常这个过程都会自动被HttpClient处理,对用户透明。用户只需要提供Http请求对象,HttpClient就会将http请求发送给目标服务器,并且接收服务器的响应,如果http请求执行不成功,httpclient就会抛出异样。

下面是个很简单的http请求执行的例子:

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpGet httpget = new HttpGet("http://localhost/");
  3. CloseableHttpResponse response = httpclient.execute(httpget);
  4. try {
  5. <...>
  6. } finally {
  7. response.close();
  8. }

1.1.1. HTTP请求

所有的Http请求都有一个请求行(request line),包括方法名、请求的URI和Http版本号。

HttpClient支持HTTP/1.1这个版本定义的所有Http方法:GET,HEAD,POST,PUT,DELETE,TRACEOPTIONS。对于每一种http方法,HttpClient都定义了一个相应的类:HttpGetHttpHeadHttpPostHttpPutHttpDeleteHttpTraceHttpOpquertions。

Request-URI即统一资源定位符,用来标明Http请求中的资源。Http request URIs包含协议名、主机名、主机端口(可选)、资源路径、query(可选)和片段信息(可选)。

  1. HttpGet httpget = new HttpGet(
  2. "http://www.google.com/search?hl=en&q=httpclient&btnG=Google+Search&aq=f&oq=");

HttpClient提供URIBuilder工具类来简化URIs的创建和修改过程。

  1. URI uri = new URIBuilder()
  2. .setScheme("http")
  3. .setHost("www.google.com")
  4. .setPath("/search")
  5. .setParameter("q", "httpclient")
  6. .setParameter("btnG", "Google Search")
  7. .setParameter("aq", "f")
  8. .setParameter("oq", "")
  9. .build();
  10. HttpGet httpget = new HttpGet(uri);
  11. System.out.println(httpget.getURI());

上述代码会在控制台输出:

  1. http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

1.1.2. HTTP响应

服务器收到客户端的http请求后,就会对其进行解析,然后把响应发给客户端,这个响应就是HTTP response.HTTP响应第一行是协议版本,之后是数字状态码和相关联的文本段。

  1. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  2. HttpStatus.SC_OK, "OK");
  3. System.out.println(response.getProtocolVersion());
  4. System.out.println(response.getStatusLine().getStatusCode());
  5. System.out.println(response.getStatusLine().getReasonPhrase());
  6. System.out.println(response.getStatusLine().toString());

上述代码会在控制台输出:

  1. HTTP/1.1
  2. 200
  3. OK
  4. HTTP/1.1 200 OK

1.1.3. 消息头

一个Http消息可以包含一系列的消息头,用来对http消息进行描述,比如消息长度,消息类型等等。HttpClient提供了方法来获取、添加、移除、枚举消息头。

  1. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  2. HttpStatus.SC_OK, "OK");
  3. response.addHeader("Set-Cookie",
  4. "c1=a; path=/; domain=localhost");
  5. response.addHeader("Set-Cookie",
  6. "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  7. Header h1 = response.getFirstHeader("Set-Cookie");
  8. System.out.println(h1);
  9. Header h2 = response.getLastHeader("Set-Cookie");
  10. System.out.println(h2);
  11. Header[] hs = response.getHeaders("Set-Cookie");
  12. System.out.println(hs.length);

上述代码会在控制台输出:

  1. Set-Cookie: c1=a; path=/; domain=localhost
  2. Set-Cookie: c2=b; path="/", c3=c; domain="localhost"
  3. 2

最有效的获取指定类型的消息头的方法还是使用HeaderIterator接口。

  1. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  2. HttpStatus.SC_OK, "OK");
  3. response.addHeader("Set-Cookie",
  4. "c1=a; path=/; domain=localhost");
  5. response.addHeader("Set-Cookie",
  6. "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  7. HeaderIterator it = response.headerIterator("Set-Cookie");
  8. while (it.hasNext()) {
  9. System.out.println(it.next());
  10. }

上述代码会在控制台输出:

  1. Set-Cookie: c1=a; path=/; domain=localhost
  2. Set-Cookie: c2=b; path="/", c3=c; domain="localhost"

HeaderIterator也提供非常便捷的方式,将Http消息解析成单独的消息头元素。

  1. HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
  2. HttpStatus.SC_OK, "OK");
  3. response.addHeader("Set-Cookie",
  4. "c1=a; path=/; domain=localhost");
  5. response.addHeader("Set-Cookie",
  6. "c2=b; path=\"/\", c3=c; domain=\"localhost\"");
  7. HeaderElementIterator it = new BasicHeaderElementIterator(
  8. response.headerIterator("Set-Cookie"));
  9. while (it.hasNext()) {
  10. HeaderElement elem = it.nextElement();
  11. System.out.println(elem.getName() + " = " + elem.getValue());
  12. NameValuePair[] params = elem.getParameters();
  13. for (int i = 0; i < params.length; i++) {
  14. System.out.println(" " + params[i]);
  15. }
  16. }

上述代码会在控制台输出:

  1. c1 = a
  2. path=/
  3. domain=localhost
  4. c2 = b
  5. path=/
  6. c3 = c
  7. domain=localhost

1.1.4. HTTP实体

Http消息可以携带http实体,这个http实体既可以是http请求,也可以是http响应的。Http实体,可以在某些http请求或者响应中发现,但不是必须的。Http规范中定义了两种包含请求的方法:POST和PUT。HTTP响应一般会包含一个内容实体。当然这条规则也有异常情况,如Head方法的响应,204没有内容,304没有修改或者205内容资源重置。

HttpClient根据来源的不同,划分了三种不同的Http实体内容。

  • streamed流式: 内容是通过流来接受或者在运行中产生。特别是,streamed这一类包含从http响应中获取的实体内容。一般说来,streamed实体是不可重复的。
  • self-contained自我包含式:内容在内存中或通过独立的连接或其它实体中获得。self-contained类型的实体内容通常是可重复的。这种类型的实体通常用于关闭http请求。
  • wrapping包装式: 这种类型的内容是从另外的http实体中获取的。

当从Http响应中读取内容时,上面的三种区分对于连接管理器来说是非常重要的。对于由应用程序创建而且只使用HttpClient发送的请求实体,streamed和self-contained两种类型的不同就不那么重要了。这种情况下,建议考虑如streamed流式这种不能重复的实体,和可以重复的self-contained自我包含式实体。

1.1.4.1. 可重复的实体

一个实体是可重复的,也就是说它的包含的内容可以被多次读取。这种多次读取只有self contained(自包含)的实体能做到(比如ByteArrayEntity或者StringEntity)。

1.1.4.2. 使用Http实体

由于一个Http实体既可以表示二进制内容,又可以表示文本内容,所以Http实体要支持字符编码(为了支持后者,即文本内容)。

当需要执行一个完整内容的Http请求或者Http请求已经成功,服务器要发送响应到客户端时,Http实体就会被创建。

如果要从Http实体中读取内容,我们可以利用HttpEntity类的getContent方法来获取实体的输入流(java.io.InputStream),或者利用HttpEntity类的writeTo(OutputStream)方法来获取输出流,这个方法会把所有的内容写入到给定的流中。
当实体类已经被接受后,我们可以利用HttpEntity类的getContentType()getContentLength()方法来读取Content-TypeContent-Length两个头消息(如果有的话)。由于Content-Type包含mime-types的字符编码,比如text/plain或者text/html,HttpEntity类的getContentEncoding()方法就是读取这个编码的。如果头信息不存在,getContentLength()会返回-1,getContentType()会返回NULL。如果Content-Type信息存在,就会返回一个Header类。

当为发送消息创建Http实体时,需要同时附加meta信息。

  1. StringEntity myEntity = new StringEntity("important message",
  2. ContentType.create("text/plain", "UTF-8"));
  3. System.out.println(myEntity.getContentType());
  4. System.out.println(myEntity.getContentLength());
  5. System.out.println(EntityUtils.toString(myEntity));
  6. System.out.println(EntityUtils.toByteArray(myEntity).length);

上述代码会在控制台输出:

  1. Content-Type: text/plain; charset=utf-8
  2. 17
  3. important message
  4. 17

1.1.5. 确保底层的资源连接被释放

为了确保系统资源被正确地释放,我们要么管理Http实体的内容流、要么关闭Http响应。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpGet httpget = new HttpGet("http://localhost/");
  3. CloseableHttpResponse response = httpclient.execute(httpget);
  4. try {
  5. HttpEntity entity = response.getEntity();
  6. if (entity != null) {
  7. InputStream instream = entity.getContent();
  8. try {
  9. // do something useful
  10. } finally {
  11. instream.close();
  12. }
  13. }
  14. } finally {
  15. response.close();
  16. }

关闭Http实体内容流和关闭Http响应的区别在于,前者通过消耗掉Http实体内容来保持相关的http连接,然后后者会立即关闭、丢弃http连接。

请注意HttpEntitywriteTo(OutputStream)方法,当Http实体被写入到OutputStream后,也要确保释放系统资源。如果这个方法内调用了HttpEntitygetContent()方法,那么它会有一个java.io.InpputStream的实例,我们需要在finally中关闭这个流。

但是也有这样的情况,我们只需要获取Http响应内容的一小部分,而获取整个内容并、实现连接的可重复性代价太大,这时我们可以通过关闭响应的方式来关闭内容输入、输出流。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpGet httpget = new HttpGet("http://localhost/");
  3. CloseableHttpResponse response = httpclient.execute(httpget);
  4. try {
  5. HttpEntity entity = response.getEntity();
  6. if (entity != null) {
  7. InputStream instream = entity.getContent();
  8. int byteOne = instream.read();
  9. int byteTwo = instream.read();
  10. // Do not need the rest
  11. }
  12. } finally {
  13. response.close();
  14. }

上面的代码执行后,连接变得不可用,所有的资源都将被释放。

1.1.6. 消耗HTTP实体内容

HttpClient推荐使用HttpEntitygetConent()方法或者HttpEntitywriteTo(OutputStream)方法来消耗掉Http实体内容。HttpClient也提供了EntityUtils这个类,这个类提供一些静态方法可以更容易地读取Http实体的内容和信息。和以java.io.InputStream流读取内容的方式相比,EntityUtils提供的方法可以以字符串或者字节数组的形式读取Http实体。但是,强烈不推荐使用EntityUtils这个类,除非目标服务器发出的响应是可信任的,并且http响应实体的长度不会过大。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpGet httpget = new HttpGet("http://localhost/");
  3. CloseableHttpResponse response = httpclient.execute(httpget);
  4. try {
  5. HttpEntity entity = response.getEntity();
  6. if (entity != null) {
  7. long len = entity.getContentLength();
  8. if (len != -1 && len < 2048) {
  9. System.out.println(EntityUtils.toString(entity));
  10. } else {
  11. // Stream content out
  12. }
  13. }
  14. } finally {
  15. response.close();
  16. }

有些情况下,我们希望可以重复读取Http实体的内容。这就需要把Http实体内容缓存在内存或者磁盘上。最简单的方法就是把Http Entity转化成BufferedHttpEntity,这样就把原Http实体的内容缓冲到了内存中。后面我们就可以重复读取BufferedHttpEntity中的内容。

  1. CloseableHttpResponse response = <...>
  2. HttpEntity entity = response.getEntity();
  3. if (entity != null) {
  4. entity = new BufferedHttpEntity(entity);
  5. }

1.1.7. 创建HTTP实体内容

HttpClient提供了一些类,这些类可以通过http连接高效地输出Http实体内容。HttpClient提供的这几个类涵盖的常见的数据类型,如String,byte数组,输入流,和文件类型:StringEntity,ByteArrayEntity,InputStreamEntity,FileEntity

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

请注意由于InputStreamEntity只能从下层的数据流中读取一次,所以它是不能重复的。推荐,通过继承HttpEntity这个自包含的类来自定义HttpEntity类,而不是直接使用InputStreamEntity这个类。FileEntity就是一个很好的起点(FileEntity就是继承的HttpEntity)。

1.7.1.1. HTML表单

很多应用程序需要模拟提交Html表单的过程,举个例子,登陆一个网站或者将输入内容提交给服务器。HttpClient提供了UrlEncodedFormEntity这个类来帮助实现这一过程。

  1. List<NameValuePair> formparams = new ArrayList<NameValuePair>();
  2. formparams.add(new BasicNameValuePair("param1", "value1"));
  3. formparams.add(new BasicNameValuePair("param2", "value2"));
  4. UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
  5. HttpPost httppost = new HttpPost("http://localhost/handler.do");
  6. httppost.setEntity(entity);

UrlEncodedFormEntity实例会使用所谓的Url编码的方式对我们的参数进行编码,产生的结果如下:

  1. param1=value1&m2=value2

1.1.7.2. 内容分块

一般来说,推荐让HttpClient自己根据Http消息传递的特征来选择最合适的传输编码。当然,如果非要手动控制也是可以的,可以通过设置HttpEntitysetChunked()为true。请注意:HttpClient仅会将这个参数看成是一个建议。如果Http的版本(如http 1.0)不支持内容分块,那么这个参数就会被忽略。

  1. StringEntity entity = new StringEntity("important message",
  2. ContentType.create("plain/text", Consts.UTF_8));
  3. entity.setChunked(true);
  4. HttpPost httppost = new HttpPost("http://localhost/acrtion.do");
  5. httppost.setEntity(entity);

1.1.8.RESPONSE HANDLERS

最简单也是最方便的处理http响应的方法就是使用ResponseHandler接口,这个接口中有handleResponse(HttpResponse response)方法。使用这个方法,用户完全不用关心http连接管理器。当使用ResponseHandler时,HttpClient会自动地将Http连接释放给Http管理器,即使http请求失败了或者抛出了异常。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpGet httpget = new HttpGet("http://localhost/json");
  3. ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() {
  4. @Override
  5. public JsonObject handleResponse(
  6. final HttpResponse response) throws IOException {
  7. StatusLine statusLine = response.getStatusLine();
  8. HttpEntity entity = response.getEntity();
  9. if (statusLine.getStatusCode() >= 300) {
  10. throw new HttpResponseException(
  11. statusLine.getStatusCode(),
  12. statusLine.getReasonPhrase());
  13. }
  14. if (entity == null) {
  15. throw new ClientProtocolException("Response contains no content");
  16. }
  17. Gson gson = new GsonBuilder().create();
  18. ContentType contentType = ContentType.getOrDefault(entity);
  19. Charset charset = contentType.getCharset();
  20. Reader reader = new InputStreamReader(entity.getContent(), charset);
  21. return gson.fromJson(reader, MyJsonObject.class);
  22. }
  23. };
  24. MyJsonObject myjson = client.execute(httpget, rh);

1.2. HttpClient接口

对于Http请求执行过程来说,HttpClient的接口有着必不可少的作用。HttpClient接口没有对Http请求的过程做特别的限制和详细的规定,连接管理、状态管理、授权信息和重定向处理这些功能都单独实现。这样用户就可以更简单地拓展接口的功能(比如缓存响应内容)。

一般说来,HttpClient实际上就是一系列特殊的handler或者说策略接口的实现,这些handler(测试接口)负责着处理Http协议的某一方面,比如重定向、认证处理、有关连接持久性和keep alive持续时间的决策。这样就允许用户使用自定义的参数来代替默认配置,实现个性化的功能。

  1. ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() {
  2. @Override
  3. public long getKeepAliveDuration(
  4. HttpResponse response,
  5. HttpContext context) {
  6. long keepAlive = super.getKeepAliveDuration(response, context);
  7. if (keepAlive == -1) {
  8. // Keep connections alive 5 seconds if a keep-alive value
  9. // has not be explicitly set by the server
  10. keepAlive = 5000;
  11. }
  12. return keepAlive;
  13. }
  14. };
  15. CloseableHttpClient httpclient = HttpClients.custom()
  16. .setKeepAliveStrategy(keepAliveStrat)
  17. .build();

1.2.1.HTTPCLIENT的线程安全性

HttpClient已经实现了线程安全。所以希望用户在实例化HttpClient时,也要支持为多个请求使用。

1.2.2.HTTPCLIENT的内存分配

当一个CloseableHttpClient的实例不再被使用,并且它的作用范围即将失效,和它相关的连接必须被关闭,关闭方法可以调用CloseableHttpClientclose()方法。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. try {
  3. <...>
  4. } finally {
  5. httpclient.close();
  6. }

1.3.Http执行上下文

最初,Http被设计成一种无状态的、面向请求-响应的协议。然而,在实际使用中,我们希望能够在一些逻辑相关的请求-响应中,保持状态信息。为了使应用程序可以保持Http的持续状态,HttpClient允许http连接在特定的Http上下文中执行。如果在持续的http请求中使用了同样的上下文,那么这些请求就可以被分配到一个逻辑会话中。HTTP上下文就和一个java.util.Map<String, Object>功能类似。它实际上就是一个任意命名的值的集合。应用程序可以在Http请求执行前填充上下文的值,也可以在请求执行完毕后检查上下文。

HttpContext可以包含任意类型的对象,因此如果在多线程中共享上下文会不安全。推荐每个线程都只包含自己的http上下文。

在Http请求执行的过程中,HttpClient会自动添加下面的属性到Http上下文中:

  • HttpConnection的实例,表示客户端与服务器之间的连接
  • HttpHost的实例,表示要连接的目标服务器
  • HttpRoute的实例,表示全部的连接路由
  • HttpRequest的实例,表示Http请求。在执行上下文中,最终的HttpRequest对象会代表http消息的状态。Http/1.0和Http/1.1都默认使用相对的uri。但是如果使用了非隧道模式的代理服务器,就会使用绝对路径的uri。
  • HttpResponse的实例,表示Http响应
  • java.lang.Boolean对象,表示是否请求被成功的发送给目标服务器
  • RequestConfig对象,表示http request的配置信息
  • java.util.List<Uri>对象,表示Http响应中的所有重定向地址

我们可以使用HttpClientContext这个适配器来简化和上下文交互的过程。

  1. HttpContext context = <...>
  2. HttpClientContext clientContext = HttpClientContext.adapt(context);
  3. HttpHost target = clientContext.getTargetHost();
  4. HttpRequest request = clientContext.getRequest();
  5. HttpResponse response = clientContext.getResponse();
  6. RequestConfig config = clientContext.getRequestConfig();

同一个逻辑会话中的多个Http请求,应该使用相同的Http上下文来执行,这样就可以自动地在http请求中传递会话上下文和状态信息。
在下面的例子中,我们在开头设置的参数,会被保存在上下文中,并且会应用到后续的http请求中。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. RequestConfig requestConfig = RequestConfig.custom()
  3. .setSocketTimeout(1000)
  4. .setConnectTimeout(1000)
  5. .build();
  6. HttpGet httpget1 = new HttpGet("http://localhost/1");
  7. httpget1.setConfig(requestConfig);
  8. CloseableHttpResponse response1 = httpclient.execute(httpget1, context);
  9. try {
  10. HttpEntity entity1 = response1.getEntity();
  11. } finally {
  12. response1.close();
  13. }
  14. HttpGet httpget2 = new HttpGet("http://localhost/2");
  15. CloseableHttpResponse response2 = httpclient.execute(httpget2, context);
  16. try {
  17. HttpEntity entity2 = response2.getEntity();
  18. } finally {
  19. response2.close();
  20. }

1.4. 异常处理

HttpClient会被抛出两种类型的异常,一种是java.io.IOException,当遇到I/O异常时抛出(socket超时,或者socket被重置);另一种是HttpException,表示Http失败,如Http协议使用不正确。通常认为,I/O错误时不致命、可修复的,而Http协议错误是致命了,不能自动修复的错误。

1.4.1.HTTP传输安全

Http协议不能满足所有类型的应用场景,我们需要知道这点。Http是个简单的面向协议的请求/响应的协议,当初它被设计用来支持静态或者动态生成的内容检索,之前从来没有人想过让它支持事务性操作。例如,Http服务器成功接收、处理请求后,生成响应消息,并且把状态码发送给客户端,这个过程是Http协议应该保证的。但是,如果客户端由于读取超时、取消请求或者系统崩溃导致接收响应失败,服务器不会回滚这一事务。如果客户端重新发送这个请求,服务器就会重复的解析、执行这个事务。在一些情况下,这会导致应用程序的数据损坏和应用程序的状态不一致。

即使Http当初设计是不支持事务操作,但是它仍旧可以作为传输协议为某些关键程序提供服务。为了保证Http传输层的安全性,系统必须保证应用层上的http方法的幂等性。

1.4.2.方法的幂等性

HTTP/1.1规范中是这样定义幂等方法的,Methods can also have the property of “idempotence” in that (aside from error or expiration issues) the side-effects of N > 0 identical requests is the same as for a single request。用其他话来说,应用程序需要正确地处理同一方法多次执行造成的影响。添加一个具有唯一性的id就能避免重复执行同一个逻辑请求,问题解决。

请知晓,这个问题不只是HttpClient才会有,基于浏览器的应用程序也会遇到Http方法不幂等的问题。

HttpClient默认把非实体方法gethead方法看做幂等方法,把实体方法postput方法看做非幂等方法。

1.4.3.异常自动修复

默认情况下,HttpClient会尝试自动修复I/O异常。这种自动修复仅限于修复几个公认安全的异常。

  • HttpClient不会尝试修复任何逻辑或者http协议错误(即从HttpException衍生出来的异常)。
  • HttpClient会自动再次发送幂等的方法(如果首次执行失败)。
  • HttpClient会自动再次发送遇到transport异常的方法,前提是Http请求仍旧保持着连接(例如http请求没有全部发送给目标服务器,HttpClient会再次尝试发送)。

1.4.4.请求重试HANDLER

如果要自定义异常处理机制,我们需要实现HttpRequestRetryHandler接口。

  1. HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
  2. public boolean retryRequest(
  3. IOException exception,
  4. int executionCount,
  5. HttpContext context) {
  6. if (executionCount >= 5) {
  7. // Do not retry if over max retry count
  8. return false;
  9. }
  10. if (exception instanceof InterruptedIOException) {
  11. // Timeout
  12. return false;
  13. }
  14. if (exception instanceof UnknownHostException) {
  15. // Unknown host
  16. return false;
  17. }
  18. if (exception instanceof ConnectTimeoutException) {
  19. // Connection refused
  20. return false;
  21. }
  22. if (exception instanceof SSLException) {
  23. // SSL handshake exception
  24. return false;
  25. }
  26. HttpClientContext clientContext = HttpClientContext.adapt(context);
  27. HttpRequest request = clientContext.getRequest();
  28. boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
  29. if (idempotent) {
  30. // Retry if the request is considered idempotent
  31. return true;
  32. }
  33. return false;
  34. }
  35. };
  36. CloseableHttpClient httpclient = HttpClients.custom()
  37. .setRetryHandler(myRetryHandler)
  38. .build();

1.5.终止请求

有时候由于目标服务器负载过高或者客户端目前有太多请求积压,http请求不能在指定时间内执行完毕。这时候终止这个请求,释放阻塞I/O的进程,就显得很必要。通过HttpClient执行的Http请求,在任何状态下都能通过调用HttpUriRequestabort()方法来终止。这个方法是线程安全的,并且能在任何线程中调用。当Http请求被终止了,本线程(即使现在正在阻塞I/O)也会通过抛出一个InterruptedIOException异常,来释放资源。

1.6. Http协议拦截器

HTTP协议拦截器是一种实现一个特定的方面的HTTP协议的代码程序。通常情况下,协议拦截器会将一个或多个头消息加入到接受或者发送的消息中。协议拦截器也可以操作消息的内容实体—消息内容的压缩/解压缩就是个很好的例子。通常,这是通过使用“装饰”开发模式,一个包装实体类用于装饰原来的实体来实现。一个拦截器可以合并,形成一个逻辑单元。

协议拦截器可以通过共享信息协作——比如处理状态——通过HTTP执行上下文。协议拦截器可以使用Http上下文存储一个或者多个连续请求的处理状态。

通常,只要拦截器不依赖于一个特定状态的http上下文,那么拦截执行的顺序就无所谓。如果协议拦截器有相互依赖关系,必须以特定的顺序执行,那么它们应该按照特定的顺序加入到协议处理器中。

协议处理器必须是线程安全的。类似于servlets,协议拦截器不应该使用变量实体,除非访问这些变量是同步的(线程安全的)。

下面是个例子,讲述了本地的上下文时如何在连续请求中记录处理状态的:

  1. CloseableHttpClient httpclient = HttpClients.custom()
  2. .addInterceptorLast(new HttpRequestInterceptor() {
  3. public void process(
  4. final HttpRequest request,
  5. final HttpContext context) throws HttpException, IOException {
  6. AtomicInteger count = (AtomicInteger) context.getAttribute("count");
  7. request.addHeader("Count", Integer.toString(count.getAndIncrement()));
  8. }
  9. })
  10. .build();
  11. AtomicInteger count = new AtomicInteger(1);
  12. HttpClientContext localContext = HttpClientContext.create();
  13. localContext.setAttribute("count", count);
  14. HttpGet httpget = new HttpGet("http://localhost/");
  15. for (int i = 0; i < 10; i++) {
  16. CloseableHttpResponse response = httpclient.execute(httpget, localContext);
  17. try {
  18. HttpEntity entity = response.getEntity();
  19. } finally {
  20. response.close();
  21. }
  22. }

上面代码在发送http请求时,会自动添加Count这个header,可以使用wireshark抓包查看。

1.7.1. 重定向处理

HttpClient会自动处理所有类型的重定向,除了那些Http规范明确禁止的重定向。See Other (status code 303) redirects on POST and PUT requests are converted to GET requests as required by the HTTP specification. 我们可以使用自定义的重定向策略来放松Http规范对Post方法重定向的限制。

  1. LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy();
  2. CloseableHttpClient httpclient = HttpClients.custom()
  3. .setRedirectStrategy(redirectStrategy)
  4. .build();

HttpClient在请求执行过程中,经常需要重写请求的消息。 HTTP/1.0和HTTP/1.1都默认使用相对的uri路径。同样,原始的请求可能会被一次或者多次的重定向。最终结对路径的解释可以使用最初的请求和上下文。URIUtils类的resolve方法可以用于将拦截的绝对路径构建成最终的请求。这个方法包含了最后一个分片标识符或者原始请求。

  1. CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpClientContext context = HttpClientContext.create();
  3. HttpGet httpget = new HttpGet("http://localhost:8080/");
  4. CloseableHttpResponse response = httpclient.execute(httpget, context);
  5. try {
  6. HttpHost target = context.getTargetHost();
  7. List<URI> redirectLocations = context.getRedirectLocations();
  8. URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
  9. System.out.println("Final HTTP location: " + location.toASCIIString());
  10. // Expected to be an absolute URI
  11. } finally {
  12. response.close();
  13. }

第二章 连接管理

2.1.持久连接

两个主机建立连接的过程是很复杂的一个过程,涉及到多个数据包的交换,并且也很耗时间。Http连接需要的三次握手开销很大,这一开销对于比较小的http消息来说更大。但是如果我们直接使用已经建立好的http连接,这样花费就比较小,吞吐率更大。

HTTP/1.1默认就支持Http连接复用。兼容HTTP/1.0的终端也可以通过声明来保持连接,实现连接复用。HTTP代理也可以在一定时间内保持连接不释放,方便后续向这个主机发送http请求。这种保持连接不释放的情况实际上是建立的持久连接。HttpClient也支持持久连接。

2.2.HTTP连接路由

HttpClient既可以直接、又可以通过多个中转路由(hops)和目标服务器建立连接。HttpClient把路由分为三种plain(明文 ),tunneled(隧道)和layered(分层)。隧道连接中使用的多个中间代理被称作代理链。

客户端直接连接到目标主机或者只通过了一个中间代理,这种就是Plain路由。客户端通过第一个代理建立连接,通过代理链tunnelling,这种情况就是Tunneled路由。不通过中间代理的路由不可能时tunneled路由。客户端在一个已经存在的连接上进行协议分层,这样建立起来的路由就是layered路由。协议只能在隧道—>目标主机,或者直接连接(没有代理),这两种链路上进行分层。

2.2.1.路由计算

RouteInfo接口包含了数据包发送到目标主机过程中,经过的路由信息。HttpRoute类继承了RouteInfo接口,是RouteInfo的具体实现,这个类是不允许修改的。HttpTracker类也实现了RouteInfo接口,它是可变的,HttpClient会在内部使用这个类来探测到目标主机的剩余路由。HttpRouteDirector是个辅助类,可以帮助计算数据包的下一步路由信息。这个类也是在HttpClient内部使用的。

HttpRoutePlanner接口可以用来表示基于http上下文情况下,客户端到服务器的路由计算策略。HttpClient有两个HttpRoutePlanner的实现类。SystemDefaultRoutePlanner这个类基于java.net.ProxySelector,它默认使用jvm的代理配置信息,这个配置信息一般来自系统配置或者浏览器配置。DefaultProxyRoutePlanner这个类既不使用java本身的配置,也不使用系统或者浏览器的配置。它通常通过默认代理来计算路由信息。

2.2.2. 安全的HTTP连接

为了防止通过Http消息传递的信息不被未授权的第三方获取、截获,Http可以使用SSL/TLS协议来保证http传输安全,这个协议是当前使用最广的。当然也可以使用其他的加密技术。但是通常情况下,Http信息会在加密的SSL/TLS连接上进行传输。

2.3. HTTP连接管理器

2.3.1. 管理连接和连接管理器

Http连接是复杂,有状态的,线程不安全的对象,所以它必须被妥善管理。一个Http连接在同一时间只能被一个线程访问。HttpClient使用一个叫做Http连接管理器的特殊实体类来管理Http连接,这个实体类要实现HttpClientConnectionManager接口。Http连接管理器在新建http连接时,作为工厂类;管理持久http连接的生命周期;同步持久连接(确保线程安全,即一个http连接同一时间只能被一个线程访问)。Http连接管理器和ManagedHttpClientConnection的实例类一起发挥作用,ManagedHttpClientConnection实体类可以看做http连接的一个代理服务器,管理着I/O操作。如果一个Http连接被释放或者被它的消费者明确表示要关闭,那么底层的连接就会和它的代理进行分离,并且该连接会被交还给连接管理器。这是,即使服务消费者仍然持有代理的引用,它也不能再执行I/O操作,或者更改Http连接的状态。

下面的代码展示了如何从连接管理器中取得一个http连接:

  1. HttpClientContext context = HttpClientContext.create();
  2. HttpClientConnectionManager connMrg = new BasicHttpClientConnectionManager();
  3. HttpRoute route = new HttpRoute(new HttpHost("localhost", 80));
  4. // 获取新的连接. 这里可能耗费很多时间
  5. ConnectionRequest connRequest = connMrg.requestConnection(route, null);
  6. // 10秒超时
  7. HttpClientConnection conn = connRequest.get(10, TimeUnit.SECONDS);
  8. try {
  9. // 如果创建连接失败
  10. if (!conn.isOpen()) {
  11. // establish connection based on its route info
  12. connMrg.connect(conn, route, 1000, context);
  13. // and mark it as route complete
  14. connMrg.routeComplete(conn, route, context);
  15. }
  16. // 进行自己的操作.
  17. } finally {
  18. connMrg.releaseConnection(conn, null, 1, TimeUnit.MINUTES);
  19. }

如果要终止连接,可以调用ConnectionRequestcancel()方法。这个方法会解锁被ConnectionRequestget()方法阻塞的线程。

2.3.2.简单连接管理器

BasicHttpClientConnectionManager是个简单的连接管理器,它一次只能管理一个连接。尽管这个类是线程安全的,它在同一时间也只能被一个线程使用。BasicHttpClientConnectionManager会尽量重用旧的连接来发送后续的请求,并且使用相同的路由。如果后续请求的路由和旧连接中的路由不匹配,BasicHttpClientConnectionManager就会关闭当前连接,使用请求中的路由重新建立连接。如果当前的连接正在被占用,会抛出java.lang.IllegalStateException异常。

2.3.3.连接池管理器

相对BasicHttpClientConnectionManager来说,PoolingHttpClientConnectionManager是个更复杂的类,它管理着连接池,可以同时为很多线程提供http连接请求。Connections are pooled on a per route basis.当请求一个新的连接时,如果连接池有有可用的持久连接,连接管理器就会使用其中的一个,而不是再创建一个新的连接。

PoolingHttpClientConnectionManager维护的连接数在每个路由基础和总数上都有限制。默认,每个路由基础上的连接不超过2个,总连接数不能超过20。在实际应用中,这个限制可能会太小了,尤其是当服务器也使用Http协议时。

下面的例子演示了如果调整连接池的参数:

  1. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  2. // Increase max total connection to 200
  3. cm.setMaxTotal(200);
  4. // Increase default max connection per route to 20
  5. cm.setDefaultMaxPerRoute(20);
  6. // Increase max connections for localhost:80 to 50
  7. HttpHost localhost = new HttpHost("locahost", 80);
  8. cm.setMaxPerRoute(new HttpRoute(localhost), 50);
  9. CloseableHttpClient httpClient = HttpClients.custom()
  10. .setConnectionManager(cm)
  11. .build();

2.3.4.关闭连接管理器

当一个HttpClient的实例不在使用,或者已经脱离它的作用范围,我们需要关掉它的连接管理器,来关闭掉所有的连接,释放掉这些连接占用的系统资源。

  1. CloseableHttpClient httpClient = <...>
  2. httpClient.close();

2.4.多线程请求执行

当使用了请求连接池管理器(比如PoolingClientConnectionManager)后,HttpClient就可以同时执行多个线程的请求了。

PoolingClientConnectionManager会根据它的配置来分配请求连接。如果连接池中的所有连接都被占用了,那么后续的请求就会被阻塞,直到有连接被释放回连接池中。为了防止永远阻塞的情况发生,我们可以把http.conn-manager.timeout的值设置成一个整数。如果在超时时间内,没有可用连接,就会抛出ConnectionPoolTimeoutException异常。

  1. PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  2. CloseableHttpClient httpClient = HttpClients.custom()
  3. .setConnectionManager(cm)
  4. .build();
  5. // URIs to perform GETs on
  6. String[] urisToGet = {
  7. "http://www.domain1.com/",
  8. "http://www.domain2.com/",
  9. "http://www.domain3.com/",
  10. "http://www.domain4.com/"
  11. };
  12. // create a thread for each URI
  13. GetThread[] threads = new GetThread[urisToGet.length];
  14. for (int i = 0; i < threads.length; i++) {
  15. HttpGet httpget = new HttpGet(urisToGet[i]);
  16. threads[i] = new GetThread(httpClient, httpget);
  17. }
  18. // start the threads
  19. for (int j = 0; j < threads.length; j++) {
  20. threads[j].start();
  21. }
  22. // join the threads
  23. for (int j = 0; j < threads.length; j++) {
  24. threads[j].join();
  25. }

即使HttpClient的实例是线程安全的,可以被多个线程共享访问,但是仍旧推荐每个线程都要有自己专用实例的HttpContext。

下面是GetThread类的定义:

  1. static class GetThread extends Thread {
  2. private final CloseableHttpClient httpClient;
  3. private final HttpContext context;
  4. private final HttpGet httpget;
  5. public GetThread(CloseableHttpClient httpClient, HttpGet httpget) {
  6. this.httpClient = httpClient;
  7. this.context = HttpClientContext.create();
  8. this.httpget = httpget;
  9. }
  10. @Override
  11. public void run() {
  12. try {
  13. CloseableHttpResponse response = httpClient.execute(
  14. httpget, context);
  15. try {
  16. HttpEntity entity = response.getEntity();
  17. } finally {
  18. response.close();
  19. }
  20. } catch (ClientProtocolException ex) {
  21. // Handle protocol errors
  22. } catch (IOException ex) {
  23. // Handle I/O errors
  24. }
  25. }
  26. }

2.5. 连接回收策略

经典阻塞I/O模型的一个主要缺点就是只有当组侧I/O时,socket才能对I/O事件做出反应。当连接被管理器收回后,这个连接仍然存活,但是却无法监控socket的状态,也无法对I/O事件做出反馈。如果连接被服务器端关闭了,客户端监测不到连接的状态变化(也就无法根据连接状态的变化,关闭本地的socket)。

HttpClient为了缓解这一问题造成的影响,会在使用某个连接前,监测这个连接是否已经过时,如果服务器端关闭了连接,那么连接就会失效。这种过时检查并不是100%有效,并且会给每个请求增加10到30毫秒额外开销。唯一一个可行的,且does not involve a one thread per socket model for idle connections的解决办法,是建立一个监控线程,来专门回收由于长时间不活动而被判定为失效的连接。这个监控线程可以周期性的调用ClientConnectionManager类的closeExpiredConnections()方法来关闭过期的连接,回收连接池中被关闭的连接。它也可以选择性的调用ClientConnectionManager类的closeIdleConnections()方法来关闭一段时间内不活动的连接。

  1. public static class IdleConnectionMonitorThread extends Thread {
  2. private final HttpClientConnectionManager connMgr;
  3. private volatile boolean shutdown;
  4. public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
  5. super();
  6. this.connMgr = connMgr;
  7. }
  8. @Override
  9. public void run() {
  10. try {
  11. while (!shutdown) {
  12. synchronized (this) {
  13. wait(5000);
  14. // Close expired connections
  15. connMgr.closeExpiredConnections();
  16. // Optionally, close connections
  17. // that have been idle longer than 30 sec
  18. connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
  19. }
  20. }
  21. } catch (InterruptedException ex) {
  22. // terminate
  23. }
  24. }
  25. public void shutdown() {
  26. shutdown = true;
  27. synchronized (this) {
  28. notifyAll();
  29. }
  30. }
  31. }

2.6. 连接存活策略

Http规范没有规定一个持久连接应该保持存活多久。有些Http服务器使用非标准的Keep-Alive头消息和客户端进行交互,服务器端会保持数秒时间内保持连接。HttpClient也会利用这个头消息。如果服务器返回的响应中没有包含Keep-Alive头消息,HttpClient会认为这个连接可以永远保持。然而,很多服务器都会在不通知客户端的情况下,关闭一定时间内不活动的连接,来节省服务器资源。在某些情况下默认的策略显得太乐观,我们可能需要自定义连接存活策略。

  1. ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
  2. public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
  3. // Honor 'keep-alive' header
  4. HeaderElementIterator it = new BasicHeaderElementIterator(
  5. response.headerIterator(HTTP.CONN_KEEP_ALIVE));
  6. while (it.hasNext()) {
  7. HeaderElement he = it.nextElement();
  8. String param = he.getName();
  9. String value = he.getValue();
  10. if (value != null && param.equalsIgnoreCase("timeout")) {
  11. try {
  12. return Long.parseLong(value) * 1000;
  13. } catch(NumberFormatException ignore) {
  14. }
  15. }
  16. }
  17. HttpHost target = (HttpHost) context.getAttribute(
  18. HttpClientContext.HTTP_TARGET_HOST);
  19. if ("www.naughty-server.com".equalsIgnoreCase(target.getHostName())) {
  20. // Keep alive for 5 seconds only
  21. return 5 * 1000;
  22. } else {
  23. // otherwise keep alive for 30 seconds
  24. return 30 * 1000;
  25. }
  26. }
  27. };
  28. CloseableHttpClient client = HttpClients.custom()
  29. .setKeepAliveStrategy(myStrategy)
  30. .build();

2.7.socket连接工厂

Http连接使用java.net.Socket类来传输数据。这依赖于ConnectionSocketFactory接口来创建、初始化和连接socket。这样也就允许HttpClient的用户在代码运行时,指定socket初始化的代码。PlainConnectionSocketFactory是默认的创建、初始化明文socket(不加密)的工厂类。

创建socket和使用socket连接到目标主机这两个过程是分离的,所以我们可以在连接发生阻塞时,关闭socket连接。

  1. HttpClientContext clientContext = HttpClientContext.create();
  2. PlainConnectionSocketFactory sf = PlainConnectionSocketFactory.getSocketFactory();
  3. Socket socket = sf.createSocket(clientContext);
  4. int timeout = 1000; //ms
  5. HttpHost target = new HttpHost("localhost");
  6. InetSocketAddress remoteAddress = new InetSocketAddress(
  7. InetAddress.getByAddress(new byte[] {127,0,0,1}), 80);
  8. sf.connectSocket(timeout, socket, target, remoteAddress, null, clientContext);

2.7.1. 安全SOCKET分层

LayeredConnectionSocketFactoryConnectionSocketFactory的拓展接口。分层socket工厂类可以在明文socket的基础上创建socket连接。分层socket主要用于在代理服务器之间创建安全socket。HttpClient使用SSLSocketFactory这个类实现安全socket,SSLSocketFactory实现了SSL/TLS分层。请知晓,HttpClient没有自定义任何加密算法。它完全依赖于Java加密标准(JCE)和安全套接字(JSEE)拓展。

2.7.2. 集成连接管理器

自定义的socket工厂类可以和指定的协议(Http、Https)联系起来,用来创建自定义的连接管理器。

  1. ConnectionSocketFactory plainsf = <...>
  2. LayeredConnectionSocketFactory sslsf = <...>
  3. Registry<ConnectionSocketFactory> r = RegistryBuilder.<ConnectionSocketFactory>create()
  4. .register("http", plainsf)
  5. .register("https", sslsf)
  6. .build();
  7. HttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(r);
  8. HttpClients.custom()
  9. .setConnectionManager(cm)
  10. .build();

2.7.3. SSL/TLS定制

HttpClient使用SSLSocketFactory来创建ssl连接。SSLSocketFactory允许用户高度定制。它可以接受javax.net.ssl.SSLContext这个类的实例作为参数,来创建自定义的ssl连接。

  1. HttpClientContext clientContext = HttpClientContext.create();
  2. KeyStore myTrustStore = <...>
  3. SSLContext sslContext = SSLContexts.custom()
  4. .useTLS()
  5. .loadTrustMaterial(myTrustStore)
  6. .build();
  7. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);

2.7.4. 域名验证

除了信任验证和在ssl/tls协议层上进行客户端认证,HttpClient一旦建立起连接,就可以选择性验证目标域名和存储在X.509证书中的域名是否一致。这种验证可以为服务器信任提供额外的保障。X509HostnameVerifier接口代表主机名验证的策略。在HttpClient中,X509HostnameVerifier有三个实现类。重要提示:主机名有效性验证不应该和ssl信任验证混为一谈。

  • StrictHostnameVerifier: 严格的主机名验证方法和java 1.4,1.5,1.6验证方法相同。和IE6的方式也大致相同。这种验证方式符合RFC 2818通配符。The hostname must match either the first CN, or any of the subject-alts. A wildcard can occur in the CN, and in any of the subject-alts.
  • BrowserCompatHostnameVerifier: 这种验证主机名的方法,和Curl及firefox一致。The hostname must match either the first CN, or any of the subject-alts. A wildcard can occur in the CN, and in any of the subject-alts.StrictHostnameVerifierBrowserCompatHostnameVerifier方式唯一不同的地方就是,带有通配符的域名(比如*.yeetrack.com),BrowserCompatHostnameVerifier方式在匹配时会匹配所有的的子域名,包括 a.b.yeetrack.com .
  • AllowAllHostnameVerifier: 这种方式不对主机名进行验证,验证功能被关闭,是个空操作,所以它不会抛出javax.net.ssl.SSLException异常。HttpClient默认使用BrowserCompatHostnameVerifier的验证方式。如果需要,我们可以手动执行验证方式。
    1. SSLContext sslContext = SSLContexts.createSystemDefault();
    2. SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
    3. sslContext,
    4. SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);

2.8. HttpClient代理服务器配置

尽管,HttpClient支持复杂的路由方案和代理链,它同样也支持直接连接或者只通过一跳的连接。

使用代理服务器最简单的方式就是,指定一个默认的proxy参数。

  1. HttpHost proxy = new HttpHost("someproxy", 8080);
  2. DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
  3. CloseableHttpClient httpclient = HttpClients.custom()
  4. .setRoutePlanner(routePlanner)
  5. .build();

我们也可以让HttpClient去使用jre的代理服务器。

  1. SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(
  2. ProxySelector.getDefault());
  3. CloseableHttpClient httpclient = HttpClients.custom()
  4. .setRoutePlanner(routePlanner)
  5. .build();

又或者,我们也可以手动配置RoutePlanner,这样就可以完全控制Http路由的过程。

  1. HttpRoutePlanner routePlanner = new HttpRoutePlanner() {
  2. public HttpRoute determineRoute(
  3. HttpHost target,
  4. HttpRequest request,
  5. HttpContext context) throws HttpException {
  6. return new HttpRoute(target, null,  new HttpHost("someproxy", 8080),
  7. "https".equalsIgnoreCase(target.getSchemeName()));
  8. }
  9. };
  10. CloseableHttpClient httpclient = HttpClients.custom()
  11. .setRoutePlanner(routePlanner)
  12. .build();
  13. }
  14. }

第三章 Http状态管理

最初,Http被设计成一个无状态的,面向请求/响应的协议,所以它不能在逻辑相关的http请求/响应中保持状态会话。由于越来越多的系统使用http协议,其中包括http从来没有想支持的系统,比如电子商务系统。因此,http支持状态管理就很必要了。

当时的web客户端和服务器软件领先者,网景(netscape)公司,最先在他们的产品中支持http状态管理,并且制定了一些专有规范。后来,网景通过发规范草案,规范了这一机制。这些努力促成 RFC standard track制定了标准的规范。但是,现在多数的应用的状态管理机制都在使用网景公司的规范,而网景的规范和官方规定是不兼容的。因此所有的浏览器开发这都被迫兼容这两种协议,从而导致协议的不统一。

3.1. Http cookies

所谓的Http cookie就是一个token或者很短的报文信息,http代理和服务器可以通过cookie来维持会话状态。网景的工程师把它们称作“magic cookie”。

HttpClient使用Cookie接口来代表cookie。简单说来,cookie就是一个键值对。一般,cookie也会包含版本号、域名、路径和cookie有效期。

SetCookie接口可以代表服务器发给http代理的一个set-cookie响应头,在浏览器中,这个set-cookie响应头可以写入cookie,以便保持会话状态。SetCookie2接口对SetCookie接口进行了拓展,添加了Set-Cookie2方法。

ClientCookie接口继承了Cookie接口,并进行了功能拓展,比如它可以取出服务器发送过来的原始cookie的值。生成头消息是很重要的,因为只有当cookie被指定为Set-Cookie或者Set-Cookie2时,它才需要包括一些特定的属性。

3.1.1 COOKIES版本

兼容网景的规范,但是不兼容官方规范的cookie,是版本0. 兼容官方规范的版本,将会是版本1。版本1中的Cookie可能和版本0工作机制有差异。

下面的代码,创建了网景版本的Cookie:

  1. BasicClientCookie netscapeCookie = new BasicClientCookie("name", "value");
  2. netscapeCookie.setVersion(0);
  3. netscapeCookie.setDomain(".mycompany.com");
  4. netscapeCookie.setPath("/");

下面的代码,创建标准版本的Cookie。注意,标准版本的Cookie必须保留服务器发送过来的Cookie所有属性。

  1. BasicClientCookie stdCookie = new BasicClientCookie("name", "value");
  2. stdCookie.setVersion(1);
  3. stdCookie.setDomain(".mycompany.com");
  4. stdCookie.setPath("/");
  5. stdCookie.setSecure(true);
  6. // Set attributes EXACTLY as sent by the server
  7. stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1");
  8. stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".mycompany.com");

下面的代码,创建了Set-Cookie2兼容cookie。

  1. BasicClientCookie2 stdCookie = new BasicClientCookie2("name", "value");
  2. stdCookie.setVersion(1);
  3. stdCookie.setDomain(".mycompany.com");
  4. stdCookie.setPorts(new int[] {80,8080});
  5. stdCookie.setPath("/");
  6. stdCookie.setSecure(true);
  7. // Set attributes EXACTLY as sent by the server
  8. stdCookie.setAttribute(ClientCookie.VERSION_ATTR, "1");
  9. stdCookie.setAttribute(ClientCookie.DOMAIN_ATTR, ".mycompany.com");
  10. stdCookie.setAttribute(ClientCookie.PORT_ATTR, "80,8080");

3.2. Cookie规范

CookieSpec接口代表了Cookie管理规范。Cookie管理规范规定了:

  • 解析Set-CookieSet-Cookie2(可选)头消息的规则
  • 验证Cookie的规则
  • 将指定的主机名、端口和路径格式化成Cookie头消息

HttpClient有下面几种CookieSpec规范:

  • Netscape draft: 这种符合网景公司指定的规范。但是尽量不要使用,除非一定要保证兼容很旧的代码。
  • Standard: RFC 2965 HTTP状态管理规范
  • Browser compatibility: 这种方式,尽量模仿常用的浏览器,如IE和firefox
  • Best match: ‘Meta’ cookie specification that picks up a cookie policy based on the format of cookies sent with the HTTP response.它基本上将上面的几种规范积聚到一个类中。
    ++ Ignore cookies: 忽略所有Cookie
    强烈推荐使用Best Match匹配规则,让HttpClient根据运行时环境自己选择合适的规范。

3.3. 选择Cookie策略

我们可以在创建Http client的时候指定Cookie测试,如果需要,也可以在执行http请求的时候,进行覆盖指定。

  1. RequestConfig globalConfig = RequestConfig.custom()
  2. .setCookieSpec(CookieSpecs.BEST_MATCH)
  3. .build();
  4. CloseableHttpClient httpclient = HttpClients.custom()
  5. .setDefaultRequestConfig(globalConfig)
  6. .build();
  7. RequestConfig localConfig = RequestConfig.copy(globalConfig)
  8. .setCookieSpec(CookieSpecs.BROWSER_COMPATIBILITY)
  9. .build();
  10. HttpGet httpGet = new HttpGet("/");
  11. httpGet.setConfig(localConfig);

3.4. 自定义Cookie策略

如果我们要自定义Cookie测试,就要自己实现CookieSpec接口,然后创建一个CookieSpecProvider接口来新建、初始化自定义CookieSpec接口,最后把CookieSpecProvider注册到HttpClient中。一旦我们注册了自定义策略,就可以像其他标准策略一样使用了。

  1. CookieSpecProvider easySpecProvider = new CookieSpecProvider() {
  2. public CookieSpec create(HttpContext context) {
  3. return new BrowserCompatSpec() {
  4. @Override
  5. public void validate(Cookie cookie, CookieOrigin origin)
  6. throws MalformedCookieException {
  7. // Oh, I am easy
  8. }
  9. };
  10. }
  11. };
  12. Registry<CookieSpecProvider> r = RegistryBuilder.<CookieSpecProvider>create()
  13. .register(CookieSpecs.BEST_MATCH,
  14. new BestMatchSpecFactory())
  15. .register(CookieSpecs.BROWSER_COMPATIBILITY,
  16. new BrowserCompatSpecFactory())
  17. .register("easy", easySpecProvider)
  18. .build();
  19. RequestConfig requestConfig = RequestConfig.custom()
  20. .setCookieSpec("easy")
  21. .build();
  22. CloseableHttpClient httpclient = HttpClients.custom()
  23. .setDefaultCookieSpecRegistry(r)
  24. .setDefaultRequestConfig(requestConfig)
  25. .build();

3.5. Cookie持久化

HttpClient可以使用任何存储方式的cookie store,只要这个cookie store实现了CookieStore接口。默认的CookieStore通过java.util.ArrayList简单实现了BasicCookieStore。存在在BasicCookieStore中的Cookie,当载体对象被当做垃圾回收掉后,就会丢失。如果必要,用户可以自己实现更为复杂的方式。

  1. // Create a local instance of cookie store
  2. CookieStore cookieStore = new BasicCookieStore();
  3. // Populate cookies if needed
  4. BasicClientCookie cookie = new BasicClientCookie("name", "value");
  5. cookie.setVersion(0);
  6. cookie.setDomain(".mycompany.com");
  7. cookie.setPath("/");
  8. cookieStore.addCookie(cookie);
  9. // Set the store
  10. CloseableHttpClient httpclient = HttpClients.custom()
  11. .setDefaultCookieStore(cookieStore)
  12. .build();

3.6.HTTP状态管理和执行上下文

在Http请求执行过程中,HttpClient会自动向执行上下文中添加下面的状态管理对象:

  • Lookup对象 代表实际的cookie规范registry。在当前上下文中的这个值优先于默认值。
  • CookieSpec对象 代表实际的Cookie规范。
  • CookieOrigin对象 代表实际的origin server的详细信息。
  • CookieStore对象 表示Cookie store。这个属性集中的值会取代默认值。

本地的HttpContext对象可以用来在Http请求执行前,自定义Http状态管理上下文;或者测试http请求执行完毕后上下文的状态。我们也可以在不同的线程中使用不同的执行上下文。我们在http请求层指定的cookie规范集和cookie store会覆盖在http Client层级的默认值。

  1. CloseableHttpClient httpclient = <...>
  2. Lookup<CookieSpecProvider> cookieSpecReg = <...>
  3. CookieStore cookieStore = <...>
  4. HttpClientContext context = HttpClientContext.create();
  5. context.setCookieSpecRegistry(cookieSpecReg);
  6. context.setCookieStore(cookieStore);
  7. HttpGet httpget = new HttpGet("http://somehost/");
  8. CloseableHttpResponse response1 = httpclient.execute(httpget, context);
  9. <...>
  10. // Cookie origin details
  11. CookieOrigin cookieOrigin = context.getCookieOrigin();
  12. // Cookie spec used
  13. CookieSpec cookieSpec = context.getCookieSpec();

第四章 HTTP认证

HttpClient既支持HTTP标准规范定义的认证模式,又支持一些广泛使用的非标准认证模式,比如NTLM和SPNEGO。

4.1.用户凭证

任何用户认证的过程,都需要一系列的凭证来确定用户的身份。最简单的用户凭证可以是用户名和密码这种形式。UsernamePasswordCredentials这个类可以用来表示这种情况,这种凭据包含明文的用户名和密码。

这个类对于HTTP标准规范中定义的认证模式来说已经足够了。

  1. UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "pwd");
  2. System.out.println(creds.getUserPrincipal().getName());
  3. System.out.println(creds.getPassword());

上述代码会在控制台输出:

  1. user
  2. pwd

NTCredentials是微软的windows系统使用的一种凭据,包含username、password,还包括一系列其他的属性,比如用户所在的域名。在Microsoft Windows的网络环境中,同一个用户可以属于不同的域,所以他也就有不同的凭据。

  1. NTCredentials creds = new NTCredentials("user", "pwd", "workstation", "domain");
  2. System.out.println(creds.getUserPrincipal().getName());
  3. System.out.println(creds.getPassword());

上述代码输出:

  1. DOMAIN/user
  2. pwd

4.2. 认证方案

AutoScheme接口表示一个抽象的面向挑战/响应的认证方案。一个认证方案要支持下面的功能:

  • 客户端请求服务器受保护的资源,服务器会发送过来一个chanllenge(挑战),认证方案(Authentication scheme)需要解析、处理这个挑战
  • 为processed challenge提供一些属性值:认证方案的类型,和此方案需要的一些参数,这种方案适用的范围
  • 使用给定的授权信息生成授权字符串;生成http请求,用来响应服务器发送来过的授权challenge

请注意:一个认证方案可能是有状态的,因为它可能涉及到一系列的挑战/响应。

HttpClient实现了下面几种AutoScheme:

  • Basic: Basic认证方案是在RFC2617号文档中定义的。这种授权方案用明文来传输凭证信息,所以它是不安全的。虽然Basic认证方案本身是不安全的,但是它一旦和TLS/SSL加密技术结合起来使用,就完全足够了。
  • Digest: Digest(摘要)认证方案是在RFC2617号文档中定义的。Digest认证方案比Basic方案安全多了,对于那些受不了Basic+TLS/SSL传输开销的系统,digest方案是个不错的选择。
  • NTLM: NTLM认证方案是个专有的认证方案,由微软开发,并且针对windows平台做了优化。NTLM被认为比Digest更安全。
  • SPNEGO: SPNEGO(Simple and Protected GSSAPI Negotiation Mechanism)是GSSAPI的一个“伪机制”,它用来协商真正的认证机制。SPNEGO最明显的用途是在微软的HTTP协商认证机制拓展上。可协商的子机制包括NTLM、Kerberos。目前,HttpCLient只支持Kerberos机制。(原文:The negotiable sub-mechanisms include NTLM and Kerberos supported by Active Directory. At present HttpClient only supports the Kerberos sub-mechanism.)

4.3. 凭证 provider

凭证providers旨在维护一套用户的凭证,当需要某种特定的凭证时,providers就应该能产生这种凭证。认证的具体内容包括主机名、端口号、realm name和认证方案名。当使用凭据provider的时候,我们可以很模糊的指定主机名、端口号、realm和认证方案,不用写的很精确。因为,凭据provider会根据我们指定的内容,筛选出一个最匹配的方案。

只要我们自定义的凭据provider实现了CredentialsProvider这个接口,就可以在HttpClient中使用。默认的凭据provider叫做BasicCredentialsProvider,它使用java.util.HashMapCredentialsProvider进行了简单的实现。

  1. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  2. credsProvider.setCredentials(
  3. new AuthScope("somehost", AuthScope.ANY_PORT),
  4. new UsernamePasswordCredentials("u1", "p1"));
  5. credsProvider.setCredentials(
  6. new AuthScope("somehost", 8080),
  7. new UsernamePasswordCredentials("u2", "p2"));
  8. credsProvider.setCredentials(
  9. new AuthScope("otherhost", 8080, AuthScope.ANY_REALM, "ntlm"),
  10. new UsernamePasswordCredentials("u3", "p3"));
  11. System.out.println(credsProvider.getCredentials(
  12. new AuthScope("somehost", 80, "realm", "basic")));
  13. System.out.println(credsProvider.getCredentials(
  14. new AuthScope("somehost", 8080, "realm", "basic")));
  15. System.out.println(credsProvider.getCredentials(
  16. new AuthScope("otherhost", 8080, "realm", "basic")));
  17. System.out.println(credsProvider.getCredentials(
  18. new AuthScope("otherhost", 8080, null, "ntlm")));

上面代码输出:

  1. [principal: u1]
  2. [principal: u2]
  3. null
  4. [principal: u3]

4.4.HTTP授权和执行上下文

HttpClient依赖AuthState类去跟踪认证过程中的状态的详细信息。在Http请求过程中,HttpClient创建两个AuthState实例:一个用于目标服务器认证,一个用于代理服务器认证。如果服务器或者代理服务器需要用户的授权信息,AuthScopeAutoScheme和认证信息就会被填充到两个AuthScope实例中。通过对AutoState的检测,我们可以确定请求的授权类型,确定是否有匹配的AuthScheme,确定凭据provider根据指定的授权类型是否成功生成了用户的授权信息。

在Http请求执行过程中,HttpClient会向执行上下文中添加下面的授权对象:

  • Lookup对象,表示使用的认证方案。这个对象的值可以在本地上下文中进行设置,来覆盖默认值。
  • CredentialsProvider对象,表示认证方案provider,这个对象的值可以在本地上下文中进行设置,来覆盖默认值。
  • AuthState对象,表示目标服务器的认证状态,这个对象的值可以在本地上下文中进行设置,来覆盖默认值。
  • AuthState对象,表示代理服务器的认证状态,这个对象的值可以在本地上下文中进行设置,来覆盖默认值。
  • AuthCache对象,表示认证数据的缓存,这个对象的值可以在本地上下文中进行设置,来覆盖默认值。

我们可以在请求执行前,自定义本地HttpContext对象来设置需要的http认证上下文;也可以在请求执行后,再检测HttpContext的状态,来查看授权是否成功。

  1. CloseableHttpClient httpclient = <...>
  2. CredentialsProvider credsProvider = <...>
  3. Lookup<AuthSchemeProvider> authRegistry = <...>
  4. AuthCache authCache = <...>
  5. HttpClientContext context = HttpClientContext.create();
  6. context.setCredentialsProvider(credsProvider);
  7. context.setAuthSchemeRegistry(authRegistry);
  8. context.setAuthCache(authCache);
  9. HttpGet httpget = new HttpGet("http://somehost/");
  10. CloseableHttpResponse response1 = httpclient.execute(httpget, context);
  11. <...>
  12. AuthState proxyAuthState = context.getProxyAuthState();
  13. System.out.println("Proxy auth state: " + proxyAuthState.getState());
  14. System.out.println("Proxy auth scheme: " + proxyAuthState.getAuthScheme());
  15. System.out.println("Proxy auth credentials: " + proxyAuthState.getCredentials());
  16. AuthState targetAuthState = context.getTargetAuthState();
  17. System.out.println("Target auth state: " + targetAuthState.getState());
  18. System.out.println("Target auth scheme: " + targetAuthState.getAuthScheme());
  19. System.out.println("Target auth credentials: " + targetAuthState.getCredentials());

4.5. 缓存认证数据

从版本4.1开始,HttpClient就会自动缓存验证通过的认证信息。但是为了使用这个缓存的认证信息,我们必须在同一个上下文中执行逻辑相关的请求。一旦超出该上下文的作用范围,缓存的认证信息就会失效。

4.6. 抢先认证

HttpClient默认不支持抢先认证,因为一旦抢先认证被误用或者错用,会导致一系列的安全问题,比如会把用户的认证信息以明文的方式发送给未授权的第三方服务器。因此,需要用户自己根据自己应用的具体环境来评估抢先认证带来的好处和带来的风险。

即使如此,HttpClient还是允许我们通过配置来启用抢先认证,方法是提前填充认证信息缓存到上下文中,这样,以这个上下文执行的方法,就会使用抢先认证。

  1. CloseableHttpClient httpclient = <...>
  2. HttpHost targetHost = new HttpHost("localhost", 80, "http");
  3. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  4. credsProvider.setCredentials(
  5. new AuthScope(targetHost.getHostName(), targetHost.getPort()),
  6. new UsernamePasswordCredentials("username", "password"));
  7. // Create AuthCache instance
  8. AuthCache authCache = new BasicAuthCache();
  9. // Generate BASIC scheme object and add it to the local auth cache
  10. BasicScheme basicAuth = new BasicScheme();
  11. authCache.put(targetHost, basicAuth);
  12. // Add AuthCache to the execution context
  13. HttpClientContext context = HttpClientContext.create();
  14. context.setCredentialsProvider(credsProvider);
  15. context.setAuthCache(authCache);
  16. HttpGet httpget = new HttpGet("/");
  17. for (int i = 0; i < 3; i++) {
  18. CloseableHttpResponse response = httpclient.execute(
  19. targetHost, httpget, context);
  20. try {
  21. HttpEntity entity = response.getEntity();
  22. } finally {
  23. response.close();
  24. }
  25. }

4.7. NTLM认证

从版本4.1开始,HttpClient就全面支持NTLMv1、NTLMv2和NTLM2认证。当人我们可以仍旧使用外部的NTLM引擎(比如Samba开发的JCIFS库)作为与Windows互操作性程序的一部分。

4.7.1. NTLM连接持久性

相比BasicDigest认证,NTLM认证要明显需要更多的计算开销,性能影响也比较大。这也可能是微软把NTLM协议设计成有状态连接的主要原因之一。也就是说,NTLM连接一旦建立,用户的身份就会在其整个生命周期和它相关联。NTLM连接的状态性使得连接持久性更加复杂,The stateful nature of NTLM connections makes connection persistence more complex, as for the obvious reason persistent NTLM connections may not be re-used by users with a different user identity. HttpClient中标准的连接管理器就可以管理有状态的连接。但是,同一会话中逻辑相关的请求,必须使用相同的执行上下文,这样才能使用用户的身份信息。否则,HttpClient就会结束旧的连接,为了获取被NTLM协议保护的资源,而为每个HTTP请求,创建一个新的Http连接。更新关于Http状态连接的信息,点击此处。

由于NTLM连接是有状态的,一般推荐使用比较轻量级的方法来处罚NTLM认证(如GET、Head方法),然后使用这个已经建立的连接在执行相对重量级的方法,尤其是需要附件请求实体的请求(如POST、PUT请求)。

  1. CloseableHttpClient httpclient = <...>
  2. CredentialsProvider credsProvider = new BasicCredentialsProvider();
  3. credsProvider.setCredentials(AuthScope.ANY,
  4. new NTCredentials("user", "pwd", "myworkstation", "microsoft.com"));
  5. HttpHost target = new HttpHost("www.microsoft.com", 80, "http");
  6. // Make sure the same context is used to execute logically related requests
  7. HttpClientContext context = HttpClientContext.create();
  8. context.setCredentialsProvider(credsProvider);
  9. // Execute a cheap method first. This will trigger NTLM authentication
  10. HttpGet httpget = new HttpGet("/ntlm-protected/info");
  11. CloseableHttpResponse response1 = httpclient.execute(target, httpget, context);
  12. try {
  13. HttpEntity entity1 = response1.getEntity();
  14. } finally {
  15. response1.close();
  16. }
  17. // Execute an expensive method next reusing the same context (and connection)
  18. HttpPost httppost = new HttpPost("/ntlm-protected/form");
  19. httppost.setEntity(new StringEntity("lots and lots of data"));
  20. CloseableHttpResponse response2 = httpclient.execute(target, httppost, context);
  21. try {
  22. HttpEntity entity2 = response2.getEntity();
  23. } finally {
  24. response2.close();
  25. }

4.8. SPNEGO/Kerberos认证

SPNEGO(Simple and Protected GSSAPI Megotiation Mechanism),当双方均不知道对方能使用/提供什么协议的情况下,可以使用SP认证协议。这种协议在Kerberos认证方案中经常使用。It can wrap other mechanisms, however the current version in HttpClient is designed solely with Kerberos in mind.

4.8.1. 在HTTPCIENT中使用SPNEGO

SPNEGO认证方案兼容Sun java 1.5及以上版本。但是强烈推荐jdk1.6以上。Sun的JRE提供的类就已经几乎完全可以处理Kerberos和SPNEGO token。这就意味着,需要设置很多的GSS类。SpnegoScheme是个很简单的类,可以用它来handle marshalling the tokens and 读写正确的头消息。

最好的开始方法就是从示例程序中找到KerberosHttpClient.java这个文件,尝试让它运行起来。运行过程有可能会出现很多问题,但是如果人品比较高可能会顺利一点。这个文件会提供一些输出,来帮我们调试。

在Windows系统中,应该默认使用用户的登陆凭据;当然我们也可以使用kinit来覆盖这个凭据,比如$JAVA_HOME\bin\kinit testuser@AD.EXAMPLE.NET,这在我们测试和调试的时候就显得很有用了。如果想用回Windows默认的登陆凭据,删除kinit创建的缓存文件即可。

确保在krb5.conf文件中列出domain_realms。这能解决很多不必要的问题。

4.8.2. 使用GSS/JAVA KERBEROS

下面的这份文档是针对Windows系统的,但是很多信息同样适合Unix。

org.ietf.jgss这个类有很多的配置参数,这些参数大部分都在krb5.conf/krb5.ini文件中配置。更多的信息,参考此处。

login.conf文件

下面是一个基本的login.conf文件,使用于Windows平台的IIS和JBoss Negotiation模块。

系统配置文件java.security.auth.login.config可以指定login.conf文件的路径。
login.conf的内容可能会是下面的样子:

  1. com.sun.security.jgss.login {
  2. com.sun.security.auth.module.Krb5LoginModule required client=TRUE useTicketCache=true;
  3. };
  4. com.sun.security.jgss.initiate {
  5. com.sun.security.auth.module.Krb5LoginModule required client=TRUE useTicketCache=true;
  6. };
  7. com.sun.security.jgss.accept {
  8. com.sun.security.auth.module.Krb5LoginModule required client=TRUE useTicketCache=true;
  9. };

4.8.4. KRB5.CONF / KRB5.INI 文件

如果没有手动指定,系统会使用默认配置。如果要手动指定,可以在java.security.krb5.conf中设置系统变量,指定krb5.conf的路径。krb5.conf的内容可能是下面的样子:

  1. [libdefaults]
  2. default_realm = AD.EXAMPLE.NET
  3. udp_preference_limit = 1
  4. [realms]
  5. AD.EXAMPLE.NET = {
  6. kdc = KDC.AD.EXAMPLE.NET
  7. }
  8. [domain_realms]
  9. .ad.example.net=AD.EXAMPLE.NET
  10. ad.example.net=AD.EXAMPLE.NET

4.8.5. WINDOWS详细的配置

为了允许Windows使用当前用户的tickets,javax.security.auth.useSubjectCredsOnly这个系统变量应该设置成false,并且需要在Windows注册表中添加allowtgtsessionkey这个项,而且要allow session keys to be sent in the Kerberos Ticket-Granting Ticket.

Windows Server 2003和Windows 2000 SP4,配置如下:

  1. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\Parameters
  2. Value Name: allowtgtsessionkey
  3. Value Type: REG_DWORD
  4. Value: 0x01

Windows XP SP2 配置如下:

  1. HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Lsa\Kerberos\
  2. Value Name: allowtgtsessionkey
  3. Value Type: REG_DWORD
  4. Value: 0x01

第五章 快速API

5.1. Easy to use facade API

HttpClient从4.2开始支持快速api。快速api仅仅实现了HttpClient的基本功能,它只要用于一些不需要灵活性的简单场景。例如,快速api不需要用户处理连接管理和资源释放。

下面是几个使用快速api的例子:

  1. // Execute a GET with timeout settings and return response content as String.
  2. Request.Get("http://somehost/")
  3. .connectTimeout(1000)
  4. .socketTimeout(1000)
  5. .execute().returnContent().asString();
  1. // Execute a POST with the 'expect-continue' handshake, using HTTP/1.1,
  2. // containing a request body as String and return response content as byte array.
  3. Request.Post("http://somehost/do-stuff")
  4. .useExpectContinue()
  5. .version(HttpVersion.HTTP_1_1)
  6. .bodyString("Important stuff", ContentType.DEFAULT_TEXT)
  7. .execute().returnContent().asBytes();
  1. // Execute a POST with a custom header through the proxy containing a request body
  2. // as an HTML form and save the result to the file
  3. Request.Post("http://somehost/some-form")
  4. .addHeader("X-Custom-header", "stuff")
  5. .viaProxy(new HttpHost("myproxy", 8080))
  6. .bodyForm(Form.form().add("username", "vip").add("password", "secret").build())
  7. .execute().saveContent(new File("result.dump"));

如果需要在指定的安全上下文中执行某些请求,我们也可以直接使用Exector,这时候用户的认证信息就会被缓存起来,以便后续的请求使用。

  1. Executor executor = Executor.newInstance()
  2. .auth(new HttpHost("somehost"), "username", "password")
  3. .auth(new HttpHost("myproxy", 8080), "username", "password")
  4. .authPreemptive(new HttpHost("myproxy", 8080));
  5. executor.execute(Request.Get("http://somehost/"))
  6. .returnContent().asString();
  7. executor.execute(Request.Post("http://somehost/do-stuff")
  8. .useExpectContinue()
  9. .bodyString("Important stuff", ContentType.DEFAULT_TEXT))
  10. .returnContent().asString();

5.1.1. 响应处理

一般情况下,HttpClient的快速api不用用户处理连接管理和资源释放。但是,这样的话,就必须在内存中缓存这些响应消息。为了避免这一情况,建议使用使用ResponseHandler来处理Http响应。

  1. Document result = Request.Get("http://somehost/content")
  2. .execute().handleResponse(new ResponseHandler<Document>() {
  3. public Document handleResponse(final HttpResponse response) throws IOException {
  4. StatusLine statusLine = response.getStatusLine();
  5. HttpEntity entity = response.getEntity();
  6. if (statusLine.getStatusCode() >= 300) {
  7. throw new HttpResponseException(
  8. statusLine.getStatusCode(),
  9. statusLine.getReasonPhrase());
  10. }
  11. if (entity == null) {
  12. throw new ClientProtocolException("Response contains no content");
  13. }
  14. DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
  15. try {
  16. DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
  17. ContentType contentType = ContentType.getOrDefault(entity);
  18. if (!contentType.equals(ContentType.APPLICATION_XML)) {
  19. throw new ClientProtocolException("Unexpected content type:" +
  20. contentType);
  21. }
  22. String charset = contentType.getCharset();
  23. if (charset == null) {
  24. charset = HTTP.DEFAULT_CONTENT_CHARSET;
  25. }
  26. return docBuilder.parse(entity.getContent(), charset);
  27. } catch (ParserConfigurationException ex) {
  28. throw new IllegalStateException(ex);
  29. } catch (SAXException ex) {
  30. throw new ClientProtocolException("Malformed XML document", ex);
  31. }
  32. }
  33. });

第六章 HTTP缓存

6.1. 基本概念

HttpClient的缓存机制提供一个与HTTP/1.1标准兼容的缓存层 – 相当于Java的浏览器缓存。HttpClient缓存机制的实现遵循责任链(Chain of Responsibility)设计原则,默认的HttpClient是没有缓存的,有缓存机制的HttpClient可以用来临时替代默认的HttpClient,如果开启了缓存,我们的请求结果就会从缓存中获取,而不是从目标服务器中获取。如果在Get请求头中设置了If-Modified-Since或者If-None-Match参数,那么HttpClient会自动向服务器校验缓存是否过期。

HTTP/1.1版本的缓存是语义透明的,意思是无论怎样,缓存都不应该修改客户端与服务器之间传输的请求/响应数据包。因此,在existing compliant client-server relationship中使用带有缓存的HttpClient也应该是安全的。虽然缓存是客户端的一部分,但是从Http协议的角度来看,缓存机制是为了兼容透明的缓存代理。

最后,HttpClient缓存也支持RFC 5861规定的Cache-Control拓展(stale-if-error'和stale-while-revalidate`)。

当开启缓存的HttpClient执行一个Http请求时,会经过下面的步骤:

  • 检查http请求是否符合HTTP 1.1的基本要求,如果不符合就尝试修正错误。
  • 刷新该请求无效的缓存项。(Flush any cache entries which would be invalidated by this request.)
  • 检测该请求是否可以从缓存中获取。如果不能,直接将请求发送给目标服务器,获取响应并加入缓存。
  • 如果该请求可以从缓存中获取,HttpClient就尝试读取缓存中的数据。如果读取失败,就会发送请求到目标服务器,如果可能的话,就把响应缓存起来。
  • 如果HttpClient缓存的响应可以直接返回给请求,HttpClient就会构建一个包含ByteArrayEntityBasicHttpResponse对象,并将它返回给http请求。否则,HttpClient会向服务器重新校验缓存。
  • 如果HttpClient缓存的响应,向服务器校验失败,就会向服务器重新请求数据,并将其缓存起来(如果合适的话)。
    当开启缓存的HttpClient收到服务器的响应时,会经过下面的步骤:
  • 检查收到的响应是否符合协议兼容性
  • 确定收到的响应是否可以缓存
  • 如果响应是可以缓存的,HttpClient就会尽量从响应消息中读取数据(大小可以在配置文件进行配置),并且缓存起来。
  • 如果响应数据太大,缓存或者重构消耗的响应空间不够,就会直接返回响应,不进行缓存。
    需要注意的是,带有缓存的HttpClient不是HttpClient的另一种实现,而是通过向http请求执行管道中插入附加处理组件来实现的。

6.2. RFC-2616 Compliance

HttpClient的缓存机制和RFC-2626文档规定是无条件兼容的。也就是说,只要指定了MUSTMUST NOTSHOULD或者SHOULD NOT这些Http缓存规范,HttpClient的缓存层就会按照指定的方式进行缓存。即当我们使用HttpClient的缓存机制时,HttpClient的缓存模块不会产生异常动作。

6.3. 使用范例

下面的例子讲述了如何创建一个基本的开启缓存的HttpClient。并且配置了最大缓存1000个Object对象,每个对象最大占用8192字节数据。代码中出现的数据,只是为了做演示,而过不是推荐使用的配置。

  1. CacheConfig cacheConfig = CacheConfig.custom()
  2. .setMaxCacheEntries(1000)
  3. .setMaxObjectSize(8192)
  4. .build();
  5. RequestConfig requestConfig = RequestConfig.custom()
  6. .setConnectTimeout(30000)
  7. .setSocketTimeout(30000)
  8. .build();
  9. CloseableHttpClient cachingClient = CachingHttpClients.custom()
  10. .setCacheConfig(cacheConfig)
  11. .setDefaultRequestConfig(requestConfig)
  12. .build();
  13. HttpCacheContext context = HttpCacheContext.create();
  14. HttpGet httpget = new HttpGet("http://www.mydomain.com/content/");
  15. CloseableHttpResponse response = cachingClient.execute(httpget, context);
  16. try {
  17. CacheResponseStatus responseStatus = context.getCacheResponseStatus();
  18. switch (responseStatus) {
  19. case CACHE_HIT:
  20. System.out.println("A response was generated from the cache with " +
  21. "no requests sent upstream");
  22. break;
  23. case CACHE_MODULE_RESPONSE:
  24. System.out.println("The response was generated directly by the " +
  25. "caching module");
  26. break;
  27. case CACHE_MISS:
  28. System.out.println("The response came from an upstream server");
  29. break;
  30. case VALIDATED:
  31. System.out.println("The response was generated from the cache " +
  32. "after validating the entry with the origin server");
  33. break;
  34. }
  35. } finally {
  36. response.close();
  37. }

6.4. 配置

有缓存的HttpClient继承了非缓存HttpClient的所有配置项和参数(包括超时时间,连接池大小等配置项)。如果需要对缓存进行具体配置,可以初始化一个CacheConfig对象来自定义下面的参数:

  • Cache size(缓存大小). 如果后台存储支持,我们可以指定缓存的最大条数,和每个缓存中存储的response的最大size。
  • Public/private cacheing(公用/私有 缓存). 默认情况下,缓存模块会把缓存当做公用的缓存,所以缓存机制不会缓存带有授权头消息或者指定Cache-Control:private的响应。但是如果缓存只会被一个逻辑上的用户使用(和浏览器饿缓存类似),我们可能希望关闭缓存共享机制。
  • Heuristic caching(启发式缓存)。即使服务器没有明确设置缓存控制headers信息,每个RFC2616缓存也会存储一定数目的缓存。这个特征在HttpClient中默认是关闭的,如果服务器不设置控制缓存的header信息,但是我们仍然希望对响应进行缓存,就需要在HttpClient中打开这个功能。激活启发式缓存,然后使用默认的刷新时间或者自定义刷新时间。更多启发式缓存的信息,可以参考Http/1.1 RFC文档的13.2.2小节,13.2.4小节。
  • Background validation(后台校验)。HttpClient的缓存机制支持RFC5861的stale-while-revalidate指令,它允许一定数目的缓存在后台校验是否过期。我们可能需要调整可以在后台工作的最大和最小的线程数,以及设置线程在回收前最大的空闲时间。当没有足够线程来校验缓存是否过期时,我们可以指定排队队列的大小。

6.5.存储介质

默认,HttpClient缓存机制将缓存条目和缓存的response放在本地程序的jvm内存中。这样虽然提供高性能,但是当我们的程序内存有大小限制的时候,这就会变得不太合理。因为缓存的生命中期很短,如果程序重启,缓存就会失效。当前版本的HttpClient使用EhCache和memchached来存储缓存,这样就支持将缓存放到本地磁盘或者其他存储介质上。如果内存、本地磁盘、外地磁盘,都不适合你的应用程序,HttpClient也支持自定义存储介质,只需要实现HttpCacheStorage接口,然后在创建HttpClient时,使用这个接口的配置。这种情况,缓存会存储在自定义的介质中,但是you will get to reuse all of the logic surrounding HTTP/1.1 compliance and cache handling. 一般来说,可以创建出支持任何键值对指定存储(类似Java Map接口)的HttpCacheStorage,用于进行原子更新。

最后,通过一些额外的工作,还可以建立起多层次的缓存结构;磁盘中的缓存,远程memcached中的缓存,虚拟内存中的缓存,L1/L2处理器中的缓存等。

第七章  高级主题

7.1 自定义客户端连接

在特定条件下,也许需要来定制HTTP报文通过线路传递,越过了可能使用的HTTP参数来处理非标准不兼容行为的方式。比如,对于Web爬虫,它可能需要强制HttpClient接受格式错误的响应头部信息,来抢救报文的内容。

通常插入一个自定义的报文解析器的过程或定制连接实现需要几个步骤:

提供一个自定义LineParser/LineFormatter接口实现。如果需要,实现报文解析/格式化逻辑。

  1. <span style="font-family:SimSun;">class MyLineParser extends BasicLineParser {
  2. @Override
  3. public Header parseHeader(
  4. CharArrayBuffer buffer) throws ParseException {
  5. try {
  6. return super.parseHeader(buffer);
  7. } catch (ParseException ex) {
  8. // Suppress ParseException exception
  9. return new BasicHeader(buffer.toString(), null);
  10. }
  11. }
  12. }</span>

提过一个自定义的 HttpConnectionFactory 实现。替换需要自定义的默认请求/响应解析器,请求/响应格式化器。如果需要,实现不同的报文写入/读取代码。

  1. <span style="font-family:SimSun;">HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory =
  2. new ManagedHttpClientConnectionFactory(
  3. new DefaultHttpRequestWriterFactory(),
  4. new DefaultHttpResponseParserFactory(
  5. new MyLineParser(), new DefaultHttpResponseFactory()));</span>

为了创建新类的连接,提供一个自定义的ClientConnectionOperator接口实现。如果需要,实现不同的套接字初始化代码。

  1. <span style="font-family:SimSun;">PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
  2. connFactory);
  3. CloseableHttpClient httpclient = HttpClients.custom()
  4. .setConnectionManager(cm)
  5. .build();</span>

7.2 有状态的HTTP连接

HTTP规范假设session状态信息通常是以HTTP cookie格式嵌入在HTTP报文中的,因此HTTP连接通常是无状态的,这个假设在现实生活中通常是不对的。也有一些情况,当HTTP连接使用特定的用户标识或特定的安全上下文来创建时,因此不能和其它用户共享,只能由该用户重用。这样的有状态的HTTP连接的示例就是NTLM认证连接和使用客户端证书认证的SSL连接。

7.2.1 用户令牌处理器

HttpClient依赖UserTokenHandler接口来决定给定的执行上下文是否是用户指定的。如果这个上下文是用户指定的或者如果上下文没有包含任何资源或关于当前用户指定详情而是null,令牌对象由这个处理器返回,期望唯一地标识当前的用户。用户令牌将被用来保证用户指定资源不会和其它用户来共享或重用。

如果它可以从给定的执行上下文中来获得,UserTokenHandler接口的默认实现是使用主类的一个实例来代表HTTP连接的状态对象。UserTokenHandler将会使用基于如NTLM或开启的客户端认证SSL会话认证模式的用户的主连接。如果二者都不可用,那么就不会返回令牌。

如果默认的不能满足它们的需要,用户可以提供一个自定义的实现:
  1. <span style="font-family:SimSun;">CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpClientContext context = HttpClientContext.create();
  3. HttpGet httpget = new HttpGet("http://localhost:8080/");
  4. CloseableHttpResponse response = httpclient.execute(httpget, context);
  5. try {
  6. Principal principal = context.getUserToken(Principal.class);
  7. System.out.println(principal);
  8. } finally {
  9. response.close();
  10. }</span>

如果默认的不能满足需求,用户可以提供一个特定的实现:

  1. <span style="font-family:SimSun;">UserTokenHandler userTokenHandler = new UserTokenHandler() {
  2. public Object getUserToken(HttpContext context) {
  3. return context.getAttribute("my-token");
  4. }
  5. };
  6. CloseableHttpClient httpclient = HttpClients.custom()
  7. .setUserTokenHandler(userTokenHandler)
  8. .build();</span>

7.2.2 持久化有状态的连接

请注意带有状态对象的持久化连接仅当请求被执行时,相同状态对象被绑定到执行上下文时可以被重用。所以,保证相同上下文重用于执行随后的相同用户,或用户令牌绑定到之前请求执行上下文的HTTP请求是很重要的。
  1. <span style="font-family:SimSun;">CloseableHttpClient httpclient = HttpClients.createDefault();
  2. HttpClientContext context1 = HttpClientContext.create();
  3. HttpGet httpget1 = new HttpGet("http://localhost:8080/");
  4. CloseableHttpResponse response1 = httpclient.execute(httpget1, context1);
  5. try {
  6. HttpEntity entity1 = response1.getEntity();
  7. } finally {
  8. response1.close();
  9. }
  10. Principal principal = context1.getUserToken(Principal.class);
  11. HttpClientContext context2 = HttpClientContext.create();
  12. context2.setUserToken(principal);
  13. HttpGet httpget2 = new HttpGet("http://localhost:8080/");
  14. CloseableHttpResponse response2 = httpclient.execute(httpget2, context2);
  15. try {
  16. HttpEntity entity2 = response2.getEntity();
  17. } finally {
  18. response2.close();
  19. }</span>

7.3. 使用FutureRequestExecutionService

通过使用FutureRequestExecutionService,你可以调度HTTP调用以及把response当作一个Future。这是非常有用的,比如当多次调用一个Web服务。使用FutureRequestExecutionService的优势在于您可以使用多个线程来调度请求同时,对任务设置超时或取消,当response不再需要的时候。

FutureRequestExecutionService用HttpRequestFutureTask(继承FutureTask)包装request。这个类允许你取消Task以及保持跟踪各项指标,如request duration。

7.3.1. 构造FutureRequestExecutionService

futureRequestExecutionService的构造方法包括两个参数:httpClient实例和ExecutorService实例。当配置两个参数的时候,您要使用的线程数等于最大连接数是很重要的。当线程比连接多的时候,连接可能会开始超时,因为没有可用的连接。当连接多于线程时,futureRequestExecutionService不会使用所有的连接。

  1. <span style="font-family:SimSun;">HttpClient httpClient = HttpClientBuilder.create().setMaxConnPerRoute(5).build();
  2. ExecutorService executorService = Executors.newFixedThreadPool(5);
  3. FutureRequestExecutionService futureRequestExecutionService =
  4. new FutureRequestExecutionService(httpClient, executorService);</span>

7.3.2. 安排requests

要安排一个请求,只需提供一个HttpUriRequest,HttpContext和ResponseHandler。因为request是由executor service处理的,而ResponseHandler的是强制性的。

  1. <span style="font-family:SimSun;">private final class OkidokiHandler implements ResponseHandler<Boolean> {
  2. public Boolean handleResponse(
  3. final HttpResponse response) throws ClientProtocolException, IOException {
  4. return response.getStatusLine().getStatusCode() == 200;
  5. }
  6. }
  7. HttpRequestFutureTask<Boolean> task = futureRequestExecutionService.execute(
  8. new HttpGet("http://www.google.com"), HttpClientContext.create(),
  9. new OkidokiHandler());
  10. // blocks until the request complete and then returns true if you can connect to Google
  11. boolean ok=task.get();</span>

7.3.3. 取消tasks

预定的任务可能会被取消。如果任务尚未执行,但仅仅是排队等待执行,它根本就不会执行。如果任务在执行中且mayInterruptIfRunning参数被设置为true,请求中的abort()函数将被调用;否则response会简单地忽略,但该请求将被允许正常完成。任何后续调用task.get()会产生一个IllegalStateException。应当注意到,取消任务仅可以释放客户端的资源。该请求可能实际上是在服务器端正常处理。

  1. <span style="font-family:SimSun;">task.cancel(true)
  2. task.get() // throws an Exception</span>

7.3.4. 回调

不用手动调用task.get(),您也可以在请求完成时使用FutureCallback实例获取回调。这里采用的是和HttpAsyncClient相同的接口

  1. <span style="font-family:SimSun;">private final class MyCallback implements FutureCallback<Boolean> {
  2. public void failed(final Exception ex) {
  3. // do something
  4. }
  5. public void completed(final Boolean result) {
  6. // do something
  7. }
  8. public void cancelled() {
  9. // do something
  10. }
  11. }
  12. HttpRequestFutureTask<Boolean> task = futureRequestExecutionService.execute(
  13. new HttpGet("http://www.google.com"), HttpClientContext.create(),
  14. new OkidokiHandler(), new MyCallback());</span>

7.3.5. 指标

FutureRequestExecutionService通常用于大量Web服务调用的应用程序之中。为了便于例如监视或配置调整,FutureRequestExecutionService跟踪了几个指标。

HttpRequestFutureTask会提供一些方法来获得任务时间:从被安排,开始,直到结束。此外,请求和任务持续时间也是可用的。这些指标都聚集在FutureRequestExecutionService中的FutureRequestExecutionMetrics实例,可以通过FutureRequestExecutionService.metrics()获取。

  1. <span style="font-family:SimSun;">task.scheduledTime() // returns the timestamp the task was scheduled
  2. task.startedTime() // returns the timestamp when the task was started
  3. task.endedTime() // returns the timestamp when the task was done executing
  4. task.requestDuration // returns the duration of the http request
  5. task.taskDuration // returns the duration of the task from the moment it was scheduled
  6. FutureRequestExecutionMetrics metrics = futureRequestExecutionService.metrics()
  7. metrics.getActiveConnectionCount() // currently active connections
  8. metrics.getScheduledConnectionCount(); // currently scheduled connections
  9. metrics.getSuccessfulConnectionCount(); // total number of successful requests
  10. metrics.getSuccessfulConnectionAverageDuration(); // average request duration
  11. metrics.getFailedConnectionCount(); // total number of failed tasks
  12. metrics.getFailedConnectionAverageDuration(); // average duration of failed tasks
  13. metrics.getTaskCount(); // total number of tasks scheduled
  14. metrics.getRequestCount(); // total number of requests
  15. metrics.getRequestAverageDuration(); // average request duration
  16. metrics.getTaskAverageDuration(); // average task duration</span>

参考:http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html

HttpClient 4.3教程(转载) 转自:http://www.yeetrack.com/相关推荐

  1. hibernate官方新手教程 (转载)

    hibernate官方新手教程第一部分 - 第一个Hibernate程序 首先我们将创建一个简单的控制台(console-based)Hibernate程序.我们使用内置数据库(in-memory d ...

  2. 【教程转载】 火狐浏览器 实用插件一览(附插件下载地址)

    [教程转载] 火狐浏览器 实用插件一览(附插件下载地址)简单介绍一下: 火狐浏览器(firefox)最大的优点就是安全.快速.稳定. 有些人抱怨火狐的功能太少,有些网页不能正常浏览. 没关系,这些都不 ...

  3. 无师自通-自己学写脚本,小明外挂脚本代码教程 转载

    无师自通-自己学写脚本,小明外挂脚本代码教程 转载 2011-02-03 19:47:20| 分类: 石器脚本 assa | 标签:walkpos 指令 对话框 跳转 道具 |字号 订阅 下载LOFT ...

  4. Arcgis安装教程——转载

    Arcgis安装教程--转载 转自https://blog.csdn.net/qq_36213352/article/details/80646940

  5. WPF入门教程-转载

    最近为了做炫酷的UI,了解了WPF,之前一直是使用winform的,界面也是古老的不行. 在园里找到了一个大佬以前写的教程,备注一下.按照系列教程走下来,可以直接上手了. 备忘传送门>>& ...

  6. HttpClient 中文官方教程----第一章基础知识-只收录,未测试

    2019独角兽企业重金招聘Python工程师标准>>> 第一章基础知识 英文链接:http://hc.apache.org/httpcomponents-client-ga/tuto ...

  7. Windows+Git+TortoiseGit+COPSSH安装图文教程 转载

    准备工作: 1. Git-1.8.1.2-preview20130201.exe 下载地址: https://code.google.com/p/msysgit/downloads/list 2. C ...

  8. HTTP Component (Apache HttpClient) 5.0 + 教程

    文件上传下载系列 HTTP Component 5.0 + 教程 介绍 超文本传输​​协议 (HTTP) 可能是当今 Internet 上使用的最重要的协议. Web 服务.支持网络的设备和网络计算的 ...

  9. webpack4 教程 转载自陈三博客

    webpack 4 教程 by 陈三 on 2015-08-28 Topics: webpack 为什么选择 webpack React.js 项目 初始化项目 新建 HTML 创建 JavaScri ...

最新文章

  1. python【数据结构与算法】动态规划详解从背包到最长公共子序列(看不懂你来打我)
  2. Java 求1000以内的同构数_求1-10000之间的同构数
  3. 第三次学JAVA再学不好就吃翔(part52)--String类的其他功能
  4. 2012年12月21日所谓世界末日时做的第一个五年规划——成为领域内的专家
  5. 中班音乐 机器人教案_中班音乐教案《小鸡出壳》含反思
  6. 目标检测——neck组件的学习笔记
  7. 内核初始化流程start_kernel
  8. centos5.8下FastDFS分布式文件系统+redis+ImageMagick
  9. 汽车智力游戏-汽车游戏大全
  10. MSDN下载出现链接格式有误,如何解决
  11. SQL Prompt 破解教程
  12. 理光Ricoh Aficio MP C7501SP 一体机驱动
  13. 各层电子数排布规则_电子数的排布规律
  14. Kibana操作Elasticsearch-Aggregations聚合数据分析
  15. 学习js的第十三天【事件的绑定方式,执行方式】
  16. 变邻域搜索算法解决0-1背包问题
  17. 免费动态IP代理科普知识课堂—代理服务器的类型
  18. 牛客SQL练习题笔记 -- 通配符%和以扩充表的角度理解内连接
  19. (个体户)注册公众平台步骤
  20. 考研380分什么水平计算机,考研380分属于什么水平 算高分吗

热门文章

  1. C语言学习笔记【持续更新】
  2. MIT 计算机操作环境导论Missing Semester Lesson 6 Git 版本控制(Git)
  3. html撩妹小技巧,小白友好
  4. 利用photoshop制作图片壁纸
  5. 霍夫变换的基本理解(第八天)
  6. python怎么自学?职场老手教给你如何快速入门python
  7. Python基础知识点(入门基础知识点)
  8. 英语学渣如何成功逆袭?聊聊我获得海外工作的真实经历
  9. [逍遥命令] 【官方出品】模拟器命令行合集(memuc命令)
  10. 深度学习8.池化的概念