该功能由 [`Requests`](https://github.com/hsiafan/requests) 拓展而来
> Java版 Python Requests。语法简洁,链式调用,Cookies自动管理......目前拓展了请求重试。
[TOC]
# 简单示例
```
// 方式1
Map<String, Object> params = Maps.newHashMap();
params.put("k1", "v1");
params.put("k2", "v2");
String resp1 = Requests.get(url).params(params).send().readToText(); // params
String resp2 = Requests.post(url).body(params).send().readToText(); // body
String resp3 = Requests.post(url).forms(params).send().readToText(); // forms
// 方式2
String resp = Requests.post(url)
.params(
Parameter.of("k1", "v1"),
Parameter.of("k2", "v2"))
.send()
.readToText();
```
更多示例可以百度一下
# 重试示例
默认重试
```
@Test
public void http1() {
// 构造请求
RequestBuilder req = Requests.post(url).params(Parameter.of("k1", "v1"));
// 请求重试
RawResponse response = RequestsHelper.retry(req);
// 获取结果
TestRetryResponse result = response.readToJson(TestRetryResponse.class);
}
```
自定义重试
```
@Test
public void http2() {
// 构造请求
RequestBuilder builder = HttpHelper.post(url).params(Parameter.of("k1", "v1"));
// 构造重试(见下章)
Retryer<RawResponse> retryer = RetryerBuilder.<RawResponse>newBuilder().build();
// 请求
RawResponse response = HttpHelper.retry(builder, retryer);
// 获取结果
TestRetryResponse result = response.readToJson(TestRetryResponse.class);
}
```
# 验证结果
这里默认重试了3次,每次等1秒
```
[FastBoot][ WARN][08-11 14:09:15]-->[http-nio-9090-exec-9:201147][onRetry(HttpRetryer.java:76)] | - onRetry number:1 error:false result:true statusCode:404 delay:78
[FastBoot][ WARN][08-11 14:09:16]-->[http-nio-9090-exec-9:202222][onRetry(HttpRetryer.java:76)] | - onRetry number:2 error:false result:true statusCode:404 delay:1153
[FastBoot][ WARN][08-11 14:09:17]-->[http-nio-9090-exec-9:203302][onRetry(HttpRetryer.java:76)] | - onRetry number:3 error:false result:true statusCode:404 delay:2234
[FastBoot][ERROR][08-11 14:09:17]-->[http-nio-9090-exec-9:203303][runException(GlobalExceptionAdvice.java:134)] | - runException ......
com.xiesx.FastBoot.core.exception.RunException: 请求错误:http retry error
at com.xiesx.FastBoot.support.request.HttpRetryer.retry(HttpRetryer.java:92) ~[classes/:?]
```
请求成功返回:
```
{
"code":0,
"msg":"操作成功",
"data":{
},
"success":true
}
```
重试失败返回:
```
{
"code": 2000,
"msg": "请求失败:http retry error",
"success": false
}
```