HttpClient设置超时(转)
HttpClient 4.5版本设置连接超时时间-CloseableHttpClient设置Timeout(区别于4.3.2)
HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳定,我感觉4.5版本抽象后,很多API应该快稳定了。
使用HttpClient,一般都需要设置连接超时时间和获取数据超时时间。这两个参数很重要,目的是为了防止访问其他http时,由于超时导致自己的应用受影响。
4.5版本中,这两个参数的设置都抽象到了RequestConfig中,由相应的Builder构建,具体的例子如下:
- <code class="hljs go" style="font-family:'Courier New',Courier,monospace; font-size:1em">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());//得到请求结果
- HttpEntity entity = response.getEntity();//得到请求回来的数据</code>
setConnectTimeout:设置连接超时时间,单位毫秒。
setConnectionRequestTimeout:设置从connect Manager获取Connection 超时时间,单位毫秒。这个属性是新加的属性,因为目前版本是可以共享连接池的。
setSocketTimeout:请求获取数据的超时时间,单位毫秒。 如果访问一个接口,多少时间内无法返回数据,就直接放弃此次调用。
===========================================
昨天遇到一个问题需要设置CloseableHttpClient的超时时间,查了官方文档如下。
新建一个RequestConfig:
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
这个超时可以设置为客户端级别,作为所有请求的默认值:
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
.build();
Request不会继承客户端级别的请求配置,所以在自定义Request的时候,需要将客户端的默认配置拷贝过去:
HttpGet httpget = new HttpGet("http://www.apache.org/");
RequestConfig requestConfig = RequestConfig.copy(defaultRequestConfig)
.setProxy(new HttpHost("myotherproxy", 8080))
.build();
httpget.setConfig(requestConfig);
伦理片 http://www.dotdy.com/
4.3版本的超时是这样的:
public static String httpPost(String url, String jsonString) {
// 设置HTTP请求参数
String result = null;
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 10000);//设置请求超时时间 10s
StringEntity entity = new StringEntity(jsonString);
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");
httpPost.setEntity(entity);
HttpEntity resEntity = httpClient.execute(httpPost).getEntity();
result = EntityUtils.toString(resEntity, "UTF-8");
} catch (Exception e) {
logger.error("http接口调用异常:url is::" + url, e);
return null;
} finally {
httpClient.getConnectionManager().shutdown();
}
return result;
}
4.5.2版本是这样的:
public static String testTimeout(String url) {
// 设置HTTP请求参数
String result = null;
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(50000).setConnectionRequestTimeout(10000)
.setSocketTimeout(50000).build();
httpGet.setConfig(requestConfig);
try {
CloseableHttpResponse response = client.execute(httpGet);
result = EntityUtils.toString(response.getEntity(), "UTF-8");
} catch (ClientProtocolException e) {
logger.error("http接口调用异常:url is::" + url, e);
return null;
} catch (Exception e) {
logger.error("http接口调用异常:url is::" + url, e);
return null;
} finally {
try {
client.close();
} catch (IOException e) {
logger.error("http接口调用异常:url is::" + url, e);
}
}
return result;
}
转自:http://blog.csdn.net/h254532699/article/details/54342470
HttpClient设置超时(转)的更多相关文章
- android httpclient 设置超时
3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...
- HttpClient设置代理,超时,以及得到cookies
import java.net.URI; import java.util.List; import org.apache.http.HttpEntity; import org.apache.htt ...
- 转 HttpClient 设置连接超时时间
要: HttpClient 4.5版本升级后,设置超时时间的API又有新的变化,请大家关注. HttpClient升级到4.5版本后,API有很多变化,HttpClient 4之后,API一直没有太稳 ...
- HttpClient库设置超时
HttpClient库API跟Lucene一样,每个版本的API都变化很大,这有点让人头疼.就好比创建一个HttpClient对象吧,每一个版本的都不一样. 3.X是正常的Java语法 HttpCli ...
- httpClient创建对象、设置超时
从老版本和新版本进行比较说明: 1.创建HttpClient对象 3.X: HttpClient httpClient = new DefaultHttpClient(); 4.3: Closeabl ...
- HttpClient 如何设置超时时间
今天分享一个巨坑,就是 HttpClient.这玩意有多坑呢?就是每个版本都变,近日笔者深受其害. 先看一下代码,我要发送请求调用一个c++接口. public static String doPos ...
- httpclient: 设置请求的超时时间,连接超时时间等
httpclient: 设置请求的超时时间,连接超时时间等 public static void main(String[] args) throws Exception{ //创建httpclien ...
- [转]HttpClient的超时用法小记
HttpClient的超时用法小记 HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试, ...
- (五)HttpClient 连接超时及读取超时
第一节: HttpClient 连接超时及读取超时 HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接 ...
随机推荐
- [原]Redis使用场景及使用经验
Redis is an open source (BSD licensed), in-memory data structure store! 欢迎转载,转载请注明出处 刚刚结束一个游戏类的活动项目, ...
- 点广告获取一些BTC(比特币)的网站
免费获取比特币:http://freebtc.qiniudn.com/freebtc.html free btc:http://freebtc.qiniudn.com/freebtc.html ...
- <摘录>linux文件IO
这篇文章写的比较全面,也浅显易懂,备份下.转载自:http://blog.chinaunix.net/uid-27105712-id-3270102.html 在Linux 开发中,有几个关系到性能的 ...
- Linear regulator=low-cost dc/dc converter
The circuit in Figure 1 is a good choice if you need a power supply with high efficiency and you don ...
- Spring通过Gmail SMTP服务器MailSender发送电子邮件
Spring提供了一个有用的“org.springframework.mail.javamail.JavaMailSenderImpl”类,通过JavaMail API 简化邮件发送过程.这里有一个项 ...
- Android之旅十四 android中的xml文件解析
在我们做有关android项目的时候,肯定会涉及到对xml文件的解析操作.以下给大家介绍一下xml文件的解析.包括DOM.SAX.Pull以及曾经我们用到的DOM4J和JDOM: 要解析的XML文件: ...
- pytest文档12-skip跳过用例
前言 pytest.mark.skip可以标记无法在某些平台上运行的测试功能,或者您希望失败的测试功能 skip意味着只有在满足某些条件时才希望测试通过,否则pytest应该跳过运行测试. 常见示例是 ...
- comgrid获取多选值
var val = $('#cc').combobox('getValues');
- iOS:删除storyBoard,纯代码实现UITabBarController的视图切换功能
storyboard是一个很强大的编写代码的辅助工具,可以帮助布局多个视图之间的联系,既直观又能减少代码量:但是,作为一个程序员,在不使用storyboard的情况下,纯代码编写是必须的技能. 下面就 ...
- [转]nginx折腾记(HTTP性能能测试,与Apache对比)
话说nginx在大压力的环境中比apache的表现要好,于是下载了一个来折腾一下. 下载并编译安装,我的编译过程有点特别: 1.去除调试信息,修改$nginx_setup_path/auto/cc ...