[转]HttpClient的超时用法小记
HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试,在这里记录下。
测试版本为HttpClient——3.1
一:连接超时:connectionTimeout
1:指的是连接一个url的连接等待时间。
2:设置方法为:
public class TestHttpClientMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- HttpClient client = new HttpClient();
- HttpMethod method = new GetMethod(
- "http://test.com");
- client.getHttpConnectionManager().getParams()
- .setConnectionTimeout(3000);
- client.getHttpConnectionManager().getParams().setSoTimeout(3000);
- try {
- int statusCode = client.executeMethod(method);
- System.out.println(statusCode);
- byte[] responseBody = null;
- responseBody = method.getResponseBody();
- String result = new String(responseBody);
- System.out.println(result);
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
/**
* @param args
*/
public static void main(String[] args) {
HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(
"http://test.com");
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(3000);
try {
int statusCode = client.executeMethod(method);
System.out.println(statusCode); byte[] responseBody = null; responseBody = method.getResponseBody(); String result = new String(responseBody); System.out.println(result); } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3:测试的时候,将url改为一个不存在的url:“http://test.com”
4:超时时间3000ms过后,系统报出异常。
org.apache.commons.httpclient.ConnectTimeoutException: The host did not accept the connection within timeout of 3000 ms
- at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)
- at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
- at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
- at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
- at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
- at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)
at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
at org.apache.commons.httpclient.HttpClient.executeMethod(HttpClient.java:397)
二:读取数据超时:soTimeout
1:指的是连接上一个url,获取response的返回等待时间
2:设置方法
public class TestHttpClientMain {
- /**
- * @param args
- */
- public static void main(String[] args) {
- HttpClient client = new HttpClient();
- HttpMethod method = new GetMethod(
- "http://localhost:8080/firstTest.htm?method=test");
- client.getHttpConnectionManager().getParams()
- .setConnectionTimeout(3000);
- client.getHttpConnectionManager().getParams().setSoTimeout(2000);
- try {
- int statusCode = client.executeMethod(method);
- System.out.println(statusCode);
- byte[] responseBody = null;
- responseBody = method.getResponseBody();
- String result = new String(responseBody);
- System.out.println(result);
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
/**
* @param args
*/
public static void main(String[] args) {
HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(
"http://localhost:8080/firstTest.htm?method=test");
client.getHttpConnectionManager().getParams()
.setConnectionTimeout(3000);
client.getHttpConnectionManager().getParams().setSoTimeout(2000);
try {
int statusCode = client.executeMethod(method);
System.out.println(statusCode); byte[] responseBody = null; responseBody = method.getResponseBody(); String result = new String(responseBody); System.out.println(result); } catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3:测试的时候的连接url为我本地开启的一个url,http://localhost:8080/firstTest.htm?method=test
在我这个测试url里,当访问到这个链接时,线程sleep一段时间,来模拟返回response超时。
@RequestMapping(params = "method=test") //<——②
- public String testMethod(ModelMap model) {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("call testMethod method.");
- model.addAttribute("name", "test method");
- return "test";
- }
public String testMethod(ModelMap model) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("call testMethod method.");
model.addAttribute("name", "test method");
return "test";
}
4:将读取response返回超时时间设的时间比那个sleep时间短之后,运行程序给出异常:
java.net.SocketTimeoutException: Read timed out
- at java.net.SocketInputStream.socketRead0(Native Method)
- at java.net.SocketInputStream.read(Unknown Source)
- at java.io.BufferedInputStream.fill(Unknown Source)
- at java.io.BufferedInputStream.read(Unknown Source)
- at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
- at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
- at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
- at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
- at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.io.BufferedInputStream.fill(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
at org.apache.commons.httpclient.HttpMethodBase.readResponse(HttpMethodBase.java:1735)
ok,以后再写httpClient这两个超时时间一定要加上,不加就很可能悲剧的了。
[转]HttpClient的超时用法小记的更多相关文章
- linux expect, spawn用法小记
linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...
- [转载]expect spawn、linux expect 用法小记
原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...
- HttpURLConnection和HttpClient的简单用法
HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...
- Matlab norm 用法小记
Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p) Returns sum(abs(A).^p)^(1/p), for ...
- Shell常见用法小记
shell的简单使用 最近发现shell脚本在平常工作中简直算一把瑞士军刀,很多场景下用shell脚本能实现常用的简单需求,而之前都没怎么学习过shell,就趁机把shell相关的语法和常见用法总结了 ...
- (五)HttpClient 连接超时及读取超时
第一节: HttpClient 连接超时及读取超时 HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接 ...
- android httpclient 设置超时
3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...
- HttpClient连接超时及读取超时
HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接时间 所谓连接的时候 是HttpClient发送请求 ...
- httpClient连接超时设置
注: 每个HttpClinet对象设置都不一样 这里已3.x和4.x为例说明 1)3.X版本 创建连接 HttpClient httpClient=new DefaultHttpClient(); 这 ...
随机推荐
- 【风马一族_Android】适合你 --- 大概的描述
适合你:专注于解决毕业生,离校所遗留的闲置教材的去向问题的一款APP. 目前的现状:毕业生的闲置教材,被清理宿舍的阿姨.大叔所清理到垃圾场,或拿到收破烂的地方,卖掉. 在毕业季中,存在的闲置物品不只有 ...
- iOS界面布局设计
参考资料: 1. 谈谈如何学习ios 8的界面和布局设计 2. iOS 8 Auto Layout界面布局系列 3. 为iPhone 6设计自适应布局 4. 几张图弄明白iOS布局中的尺寸问题
- windows store app 读写图片
using System; using System.Threading.Tasks; using System.Runtime.InteropServices.WindowsRuntime; usi ...
- EasyUI DataGrid分页数据绑定
记录东西感觉很痛苦,总结东西很痛苦,麻烦,不过为了下次的方便和知识的牢固以后要坚持总结. EasyUI DataGrid分页数据绑定 在解决方案中新建两个文件FormMain.aspx(html也可以 ...
- .net(c#) winform文本框只能输入数字,不能其他非法字符
private void textBox3_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { //阻止从键盘输入键 ...
- 谈网页游戏外挂之用python模拟游戏(热血三国2)登陆
看web看多了,想写写页游的外挂,其实原理是一样的,就是端口不一样协议字段你不知道,而这也提高了点技术门槛,看我们来一点一点突破这些门槛,这次我们来用python发包模拟flash的客户端登陆. 以热 ...
- Microsoft AzureStorageAccount for Powershell
使用Powershell 创建的存储账户,注意StorageAccountName只能使用小写字母以及数字, -Location参考http://www.cnblogs.com/SignalTips/ ...
- Java从入门到精通——基础篇之JSTL标签
一.语言基础 EL(Expression Language)表达式,目的:为了使JSP写起来更加简单.提供了在 JSP 中简化表达式的方法. 二.分类 核心标签库:提供条件判断.属性访问.URL处理及 ...
- Oracle中的通配符
这是scott用户下的EMP表 EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO 7369 SMITH CLERK 7902 1980/12/17 800 ...
- Yii2 composer更新后报错: Calling unknown method: yii\web\UrlManager::addRules()
网上搜了好多答案,基本上都是让你在composer.json里面添加这样一段话: "extra": { "asset-installer-paths": { & ...