许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  HttpClient官方文档第五章:Fluent API详解(Java)

HttpClient官方文档第五章:Fluent API详解(Java)

阅读数 2
点赞 0
article_banner

第五章:流式 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_TEXT)

.execute().returnContent().asBytes();

// Execute a POST with a custom header through the proxy containing a request body

// as an HTML form and save the result to the file

Request.Post("http://somehost/some-form")

.addHeader("X-Custom-header", "stuff")

.viaProxy(new HttpHost("myproxy", 8080))

.bodyForm(Form.form().add("username", "vip").add("password", "secret").build())

.execute().saveContent(new File("result.dump"));

使用 Executor 在特定的需要安全认证的上下文中请求时,认证信息可以被缓存起来,这样后来的请求也可以重复使用认证信息了。

Executor executor = Executor.newInstance()

.auth(new HttpHost("somehost"), "username", "password")

.auth(new HttpHost("myproxy", 8080), "username", "password")

.authPreemptive(new HttpHost("myproxy", 8080));

executor.execute(Request.Get("http://somehost/"))

.returnContent().asString();

executor.execute(Request.Post("http://somehost/do-stuff")

.useExpectContinue()

.bodyString("Important stuff", ContentType.DEFAULT_TEXT))

.returnContent().asString();

5.1.1. 响应处理

流式接口(Fluent API) 增加了使用者对连接的管理和资源的分配上的便利性。在许多场景下,在内存中缓存过多的响应内容也会让它付出了相应的代价。因此它推荐使用 ResponseHandler 来处理 HTTP 响应以此来避免在内存中缓存响应内容。

Document result = Request.Get("http://somehost/content")

.execute().handleResponse(new ResponseHandler() {

public Document handleResponse(final HttpResponse response) throws IOException {

StatusLine statusLine = response.getStatusLine();

HttpEntity entity = response.getEntity();

if (statusLine.getStatusCode() >= 300) {

throw new HttpResponseException(

statusLine.getStatusCode(),

statusLine.getReasonPhrase());

}

if (entity == null) {

throw new ClientProtocolException("Response contains no content");

}

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder docBuilder = dbfac.newDocumentBuilder();

ContentType contentType = ContentType.getOrDefault(entity);

if (!contentType.equals(ContentType.APPLICATION_XML)) {

throw new ClientProtocolException("Unexpected content type:" +

contentType);

}

String charset = contentType.getCharset();

if (charset == null) {

charset = HTTP.DEFAULT_CONTENT_CHARSET;

}

return docBuilder.parse(entity.getContent(), charset);

} catch (ParserConfigurationException ex) {

throw new IllegalStateException(ex);

} catch (SAXException ex) {

throw new ClientProtocolException("Malformed XML document", ex);

}

}

});

d0c1501a6d8bb921cf36400dc89de69f.png


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空