HttpClient 之Fluent API 简单使用

     相比于HttpClient 之前的版本,HttpClient 4.2 提供了一组基于流接口(fluent interface)概念的更易使用的API,即Fluent API.

     为了方便使用,Fluent API只暴露了一些最基本的HttpClient功能。这样,Fluent API就将开发者从连接管理、资源释放等繁杂的操作中解放出来,从而更易进行一些HttpClient的简单操作。

实战:

    1.以下是Get请求,

String result = Request.Post("http://itommy.iteye.com/login")
                    .bodyForm(Form.form().add("name", "i111").build())
                    .execute()
                    .returnContent()
                    .asString();

    2.以下为Post请求,直接请求是有问题的

String result = Request.Post("http://itommy.iteye.com"))
                       .bodyForm(Form.form().add("phone", phone).build())
                       .execute().returnContent().asString();

需要注意的是,在访问https的时候,就会有问题了 

 

更详细的Fluent API介绍:

http://ifeve.com/httpclient-fluent-api/

http://blog.csdn.net/vector_yi/article/details/24298629

猜你喜欢

转载自itommy.iteye.com/blog/2352870