使用Apache HttpClient 4.5设置超时时间
使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间。这两个参数很重要,目的是为了防止访问其他http服务时,由于超时导致自己的应用受影响。
4.5版本中,这两个参数的设置都抽象到了RequestConfig中,由相应的Builder构建,具体的例子如下:
import java.io.IOException; import org.apache.http.Consts;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; /**
* @author
*
* @date 2017年5月18日 上午9:17:08
*
* @Description
*/
public class HttpGetUtils { /**
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
public static void main(String[] args) throws ClientProtocolException, IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet("http://stackoverflow.com/");
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setConnectionRequestTimeout(1000)
.setSocketTimeout(5000).build();
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpclient.execute(httpGet);
System.out.println(response.getStatusLine());// 得到状态行
System.out.println(EntityUtils.toString(response.getEntity(), Consts.UTF_8.name()));// 得到请求回来的数据
}
}
setConnectTimeout :设置连接超时时间,单位毫秒。
setConnectionRequestTimeout :设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
setSocketTimeout :请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
使用Apache HttpClient 4.5设置超时时间的更多相关文章
- HttpClient 如何设置超时时间
今天分享一个巨坑,就是 HttpClient.这玩意有多坑呢?就是每个版本都变,近日笔者深受其害. 先看一下代码,我要发送请求调用一个c++接口. public static String doPos ...
- httpClient创建对象、设置超时
从老版本和新版本进行比较说明: 1.创建HttpClient对象 3.X: HttpClient httpClient = new DefaultHttpClient(); 4.3: Closeabl ...
- C# UdpClient 设置超时时间
/********************************************************************** * C# UdpClient 设置超时时间 * 说明: ...
- mongodb3.6 (五)net 客户端访问mongodb设置超时时间踩过的“坑”
前言 在上一篇文章中,我们有提到net访问mongodb连接超时默认为30秒,这个时间在实际项目中肯定是太长的.而MongoClientSettings 也确是提供了超时属性,如下图: 可实际使用中, ...
- Go基础系列:为select设置超时时间
Go channel系列: channel入门 为select设置超时时间 nil channel用法示例 双层channel用法示例 指定goroutine的执行顺序 After() 谁也无法保证某 ...
- GuzzleHttp 请求设置超时时间
之前调用一个三方的 WEB API,大量的请求超时,导致 PHP 进程被占用完.整个网站一直报 504. 其中一个优化措施就是对三方 API 调用设置超时时间. use GuzzleHttp\Clie ...
- Mybatis设置超时时间
Mybatis设置超时时间 mybatis如果不指定,默认超时时间是不做限制的,默认值为0.mybatis sql配置超时时间有两种方法: 1.全局配置 在mybatis配置文件的settings节点 ...
- winform设置超时时间
); //设置超时时间 var completedTask = await Task.WhenAny(new Task(async () => { );//执行的方法示例这里用延迟代替 }), ...
- 爬虫学习笔记之为什么要设置超时时间,怎么设置(使用selenium)
一个程序没有设置超时时间,就可以说是一段有缺陷的代码. 读取超时指的就是客户端等待服务器发送请求的时间.(特定地,它指的是客户端要等待服务器发送字节之间的时间.在 99.9% 的情况下这指的是服务器发 ...
随机推荐
- 常见IT英语单词
lable标签,master精通.主人,reference参考,release发布,schema模式,component组件,persistence持久化,generate生成产生,plugin插件, ...
- java设计模式-----4、单例模式
单例模式是一种对象创建型模式,使用单例模式,可以保证为一个类只生成唯一的一个实例对象.也就是说,在整个程序空间中,该类只存在一个实例对象. 其实,GoF对单例模式的定义是:保证一个类,只有一个实例存在 ...
- Code Signal_练习题_Are Similar?
Two arrays are called similar if one can be obtained from another by swapping at most one pair of el ...
- WinFrom折线图
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- CSS,js,html
图片盗链问题使用以下meta标签解决 <meta name="referrer" content="never"> Chrome 中文界面下默认会将 ...
- git杂记-记录每次更新到仓库
git status 和 git diff的运用 git status 记录的是关于仓库文件是否有变更,例如是否被修改,是否被添加到暂村区.至于文件更改了什么内容该命令并不关心: git status ...
- [HAOI2009]逆序对数列(加强)
ZJL 的妹子序列 暴力就是 \(\Theta(n\times m)\) 如果 \(n,m \le 10^5\) ? 考虑问题的转换,设 \(a_i\) 表示 \(i\) 小的在它后面的数的个数 \( ...
- void()表达式结果是SyntaxError
void是一元运算符,他出现在操作数之前,操作数可以使任意类型,操作数会照常计算,但忽略计算结果并返回undefined. 因此在操作数具有副作用的时候使用void来让程序根据语义 console.l ...
- JDBC中常用对象介绍
JDBC中的主要类(接口) 在JDBC中常用的类有: 1.DriverManager 2.Connection 3.Statement 4.ResultSet 1.DriverManager 其实我们 ...
- 新手,再来1个 vue2入门的教程,有源码参考
在这之前有入门的,作者写的不错的, 照着来一下,也收益颇多,上个例子是基于 "dependencies": { "vue": "^2.2.6&qu ...