HTTPClient4.5.2学习笔记(五):流式请求API(Fluent API)
来源:互联网 发布:经济数据库万得 编辑:程序博客网 时间:2024/06/06 06:46
我个人感觉这个就像JQUERY的AJAX设计一样,简化一次HTTP请求的代码数量,可以像流水线一样往请求上添加需要的头、内容、参数。
5.1.请求facade API
从4.2版本开始,HttpClient自带了一个易于使用的facade API,基于fluent interface的概念。Fluent facadeAPI仅暴露了HttpClient的最基本功能,旨在用于不需要HttpClient的完全灵活性的简单用例。例如, fluent facadeAPI使用户不必处理连接管理和资源释放。
通过 HC fluent API以下是执行的HTTP请求的几个示例:
Request.Get("http://www.baidu.com") .socketTimeout(1000) .connectTimeout(1000) .body(new StringEntity("")) .addHeader("","") .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 fileRequest.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. Response 处理
fluent facade API通常减轻用户不必处理连接管理和资源释放。然而,在大多数情况下,这是以缓冲内存中的响应消息内容为代价的。强烈建议使用ResponseHandler进行HTTP响应处理,以避免在内存中缓冲的内容丢失。
Document result = Request.Get("http://somehost/content") .execute().handleResponse(new ResponseHandler<Document>() { 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().displayName(); if (charset == null) { charset = "UTF-8"; } return docBuilder.parse(entity.getContent(), charset); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } });总结:我个人感觉这个功能非常的棒,如果对于HTTP请求报文设置相对较少或者需求较为简单这种方法处理很方便,减少了很多臃肿的代码。
0 0
- HTTPClient4.5.2学习笔记(五):流式请求API(Fluent API)
- HttpClient4.2 Fluent API学习
- HttpClient4.2 Fluent API学习
- HttpClient4.2 Fluent API学习
- Entity Framework Code First (五)Fluent API - 配置关系
- 百度地图API学习笔记(五)
- Apache HttpClient4.3 Fluent API使用心得小结
- 整理HttpClient4.5的Fluent API的用法
- geometry-api-java 学习笔记(五)多边形 Polygons
- UiAutomator笔记之UiCollection API(五)
- apache fluent api
- Entity Framework Fluent API
- EF Code First 学习笔记:约定配置Data Annotations+Fluent API
- XMPP 学习笔记五 Strophe API学习
- hadoop API 学习小结(五)
- 【HttpClient4.5中文教程】【第五章 :流式(链式)API】
- 安卓学习笔记(五):任务和返回栈 跟着API指南来学习
- HTTPClient4.5.2学习笔记(一):基础篇
- POJ 2955 (dp括号匹配)
- spring WebSocket详解
- sql调优简单理解
- Mysql 查看数据表大小
- 图像处理 连通域标记
- HTTPClient4.5.2学习笔记(五):流式请求API(Fluent API)
- Android 根据屏幕的宽度来设置图片的大小,
- 自己实现Spring AOP(二)JDK代理实现AOP
- b.exp : warning LNK4070: .EXP 中的 /OUT:a.dll 指令与输出文件名“b.dll"不同;忽略指令
- P1579 哥德巴赫猜想(升级版)
- Android NDK (由原java工程 更改 为NDK 工程)
- visual studio 字体变化 半角模式变成全角模式怎么恢复
- mongos 集群无法启动
- 获取手机设备号及存在的问题