Unirest一款轻量级的HTTP客户端库

在java工程里如果需要透传什么信息给一个http接口的话,我们往往会使用HttpClient这个类,然后set很多param参数,这个类自然是很优秀的也被很多线上工程使用着,但是使用的过程中总不是那么的简便,比如需要引很多包,包与包之间还经常会有冲突,demo并不统一(每一个工程师写出来的mock http的代码有可能都是不一样的)。讲真,我在实践过程中,如果没有工具类从头开始自己写一个http的请求,还是要花很长时间的。

现在,有一个类库帮我们解决了这个问题——Unirest。

 

Unirest的特性

1.能够伪造GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS 这些请求。

2.支持同步/异步请求。

3.支持表单提交,文件上传,自定义实体。

4.支持路径参数

5.支持gzip

6.支持本地的 Basic Authentication

7.自定义超时,并发级别和代理设置。

8.为每个请求自定义默认请求头。

9.自定义HttpClientHttpAsyncClient

10.自动解析JSON

11.自定义的将返回的json数据转换成Java对象。

 

Maven:

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>

需要依赖如下的maven:

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpasyncclient</artifactId>
  <version>4.0.2</version>
</dependency>
<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpmime</artifactId>
  <version>4.3.6</version>
</dependency>
<dependency>
  <groupId>org.json</groupId>
  <artifactId>json</artifactId>
  <version>20140107</version>
</dependency>

 

创建请求

HttpResponse<JsonNod
  • 6
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值