原创出处:http://xcxn.iteye.com/blog/1827163
第六章
6.流利的API
6.1.易于使用的API门面
至于4.2版本的HttpClient是一个易于使用的门面API的基础上的概念能说一口流利的接口。流利的的门面API仅公开HttpClient的和最根本的功能是用于简单的用例HttpClient的,不需要充分的灵活性。例如,流畅的门面API处理连接管理和资源释放,减轻了用户。
这里有几个例子执行通过HC流利的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.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();
6.1.1.响应处理
一般流畅的门面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(); 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); } } });
6.1.2.异步执行
可以用流利的门面API使用后台线程异步执行多个请求。
ExecutorService threadpool = Executors.newFixedThreadPool(2); Async async = Async.newInstance().use(threadpool); Request[] requests = new Request[] { Request.Get("http://www.google.com/"), Request.Get("http://www.yahoo.com/"), Request.Get("http://www.apache.com/"), Request.Get("http://www.apple.com/") }; Queue<Future<Content>> queue = new LinkedList<Future<Content>>(); for (final Request request: requests) { Future<Content> future = async.execute(request, new FutureCallback<Content>() { public void failed(final Exception ex) { System.out.println(ex.getMessage() + ": " + request); } public void completed(final Content content) { System.out.println("Request completed: " + request); } public void cancelled() { } }); queue.add(future); } // Process the queue
相关推荐
最新HttpClient help文档,根据官方网站提供的doc制作。支持Index,Search等功能。
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
httpclient-4.2的依赖包,所有要使用的依赖包都在压缩包的lib目录下,如果只是需要依赖包,可以直接使用该目录下的jar包就可以了
httpclient4官方文档翻译版,自加书签,不错的入门资料
HttpClient4.5已经发布有一段时间了,但网上一直没有完整的中文教程 .曾经看到过一个HttpClient4.1中文教程,但有两个问题:1.并不适用当前版本; 2.错误较多,语言不通顺。所以笔者在这里发布翻译过的HttpClient4.5...
基于apache httpclient 4.2.X开发 自动处理redirect url jsoup解析response text log4j 自动生成日志 源代码基于UTF-8编码,如果出现乱码请切换到此编码 压缩包 包含所有jar文件。
httpclient-4.2-beta1.jar
包含翻译后的API文档:httpclient-4.5.6-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.httpcomponents:httpclient:4.5.6; 标签:apache、httpcomponents、httpclient、jar包、java、中文文档; 使用...
httpclient 4.5 api文档
从apache官网上整理出来的httpClient的文档,英文。
hiveserver2的jdbc调用时需要用到的两个包httpclient+httpcore. org.apache.http.*
HttpClient 4.3.6 api文档,支持目录,索引,搜索。
包含翻译后的API文档:httpclient-4.5.5-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:org.apache.httpcomponents,artifactId:httpclient,version:4.5.5 使用方法:解压翻译后的API文档,用...
包含翻译后的API文档:httpclient-4.5.13-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.httpcomponents:httpclient:4.5.13; 标签:apache、httpcomponents、httpclient、jar包、java、中文文档; ...
HttpClient中文文档,快速开发
包含翻译后的API文档:httpclient-4.2.5-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.httpcomponents:httpclient:4.2.5; 标签:apache、httpcomponents、httpclient、中文文档、jar包、java; 使用...
包含翻译后的API文档:httpclient-4.5.12-javadoc-API文档-中文(简体)版.zip; Maven坐标:org.apache.httpcomponents:httpclient:4.5.12; 标签:apache、httpcomponents、httpclient、中文文档、jar包、java; ...
文件名称:httpclient-4.5.3.zip 文件类型:ZIP 文件大小:2886KB 浏览入口:index.html文件,用浏览器打开
httpclient上传下载时,用到的jar包
HttpClient文档+jar包