guy*_*man 5 java apache httpclient
该文档指定了ExponentialBackOffSchedulingStrategy类,但在4.5版中似乎不存在。
此外,没有找到任何解释如何使用它的内容。
有人用过这样的东西吗?
如果您使用像这样的 Maven 包安装 Apache HTTP Client 4.5.x(Grails Maven 依赖语法):
compile "org.apache.httpcomponents:httpclient:4.5.1"
compile "org.apache.httpcomponents:httpcore:4.4.3"
Run Code Online (Sandbox Code Playgroud)
您需要添加另一个 jar 来获取这些类,如下所示:
compile 'org.apache.httpcomponents:httpclient-cache:4.5.1'
Run Code Online (Sandbox Code Playgroud)
请参阅https://hc.apache.org/httpcomponents-client-4.5.x/download.html
然后你可以像这样连接它:
import org.apache.http.impl.client.*;
import org.apache.http.impl.client.cache.*;
import org.apache.http.client.methods.*;
CloseableHttpClient createClient() {
CachingHttpClientBuilder hcb = new CachingHttpClientBuilder();
CacheConfig cc = CacheConfig.DEFAULT;
ExponentialBackOffSchedulingStrategy ebo = new ExponentialBackOffSchedulingStrategy(cc);
hcb.setSchedulingStrategy(ebo);
CloseableHttpClient hc = hcb.build();
return hc;
}
// You'll need to replace the URL below with something that returns a 5xx error
CloseableHttpClient client = createClient();
HttpUriRequest request = new HttpGet("http://www.example.com/throwsServerError");
for (int i=0; i<4; i++) {
CloseableHttpResponse response = client.execute(request);
println new Date().toString() + " " + response.getStatusLine().getStatusCode();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2786 次 |
| 最近记录: |