原文链接 译者[white]
第五章:流式 API
5.1 易用 API 接口
4.2版本的 HttpClient 带来了一组非常容易使用的流式 API(Fluent API) 接口。暴露的流式API(Fluent API) 接口中仅仅是 HttpClient 最基本的一些功能,这些接口是在不需要使用 HttpClient 丰富的灵活性时,为了一些简单的功能而准备的。 例如:流式接口(Fluent API) 增加了使用者对连接的管理和资源的分配上的便利性。这里有一系列通过 HttpClient 流式接口(Fluent API) 执行 HTTP 请求的示例:
// Execute a GET with timeout settings and return response content as String.
Request.Get("http://somehost/")
.connectTimeout(1000)
.socketTimeout(1000)
.execute().returnContent().asString();
// Execute a POST with the 'expect-continue' handshake, using HTTP/1.1,
// containing a request body as String and return response content as byte array.
Request.Post("http://somehost/do-stuff")
.useExpectContinue()
.version(HttpVersion.HTTP_1_1)
.bodyString("Important stuff", ContentType.DEFAULT