HttpClient在使用中有两个超时时间,是一直接触和使用的,由于上次工作中使用httpClient造成了系统悲剧的情况,特地对它的两个超时时间进行了小小的测试,在这里记录下。

测试版本为HttpClient——3.1

一:连接超时:connectionTimeout

1:指的是连接一个url的连接等待时间。

2:设置方法为:

public class TestHttpClientMain {

  1. /**
  2. * @param args
  3. */
  4. public static void main(String[] args) {
  5. HttpClient client = new HttpClient();
  6. HttpMethod method = new GetMethod(
  7. "http://test.com");
  8. client.getHttpConnectionManager().getParams()
  9. .setConnectionTimeout(3000);
  10. client.getHttpConnectionManager().getParams().setSoTimeout(3000);
  11. try {
  12. int statusCode = client.executeMethod(method);
  13. System.out.println(statusCode);
  14. byte[] responseBody = null;
  15. responseBody = method.getResponseBody();
  16. String result = new String(responseBody);
  17. System.out.println(result);
  18. } catch (HttpException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
	/**
* @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

  1. at org.apache.commons.httpclient.protocol.ReflectionSocketFactory.createSocket(ReflectionSocketFactory.java:155)
  2. at org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.createSocket(DefaultProtocolSocketFactory.java:125)
  3. at org.apache.commons.httpclient.HttpConnection.open(HttpConnection.java:707)
  4. at org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(HttpMethodDirector.java:387)
  5. at org.apache.commons.httpclient.HttpMethodDirector.executeMethod(HttpMethodDirector.java:171)
  6. 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 {

  1. /**
  2. * @param args
  3. */
  4. public static void main(String[] args) {
  5. HttpClient client = new HttpClient();
  6. HttpMethod method = new GetMethod(
  7. "http://localhost:8080/firstTest.htm?method=test");
  8. client.getHttpConnectionManager().getParams()
  9. .setConnectionTimeout(3000);
  10. client.getHttpConnectionManager().getParams().setSoTimeout(2000);
  11. try {
  12. int statusCode = client.executeMethod(method);
  13. System.out.println(statusCode);
  14. byte[] responseBody = null;
  15. responseBody = method.getResponseBody();
  16. String result = new String(responseBody);
  17. System.out.println(result);
  18. } catch (HttpException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. } catch (IOException e) {
  22. // TODO Auto-generated catch block
  23. e.printStackTrace();
  24. }
  25. }
	/**
* @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") //<——②

  1. public String testMethod(ModelMap model) {
  2. try {
  3. Thread.sleep(3000);
  4. } catch (InterruptedException e) {
  5. // TODO Auto-generated catch block
  6. e.printStackTrace();
  7. }
  8. System.out.println("call testMethod method.");
  9. model.addAttribute("name", "test method");
  10. return "test";
  11. }
    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

  1. at java.net.SocketInputStream.socketRead0(Native Method)
  2. at java.net.SocketInputStream.read(Unknown Source)
  3. at java.io.BufferedInputStream.fill(Unknown Source)
  4. at java.io.BufferedInputStream.read(Unknown Source)
  5. at org.apache.commons.httpclient.HttpParser.readRawLine(HttpParser.java:78)
  6. at org.apache.commons.httpclient.HttpParser.readLine(HttpParser.java:106)
  7. at org.apache.commons.httpclient.HttpConnection.readLine(HttpConnection.java:1116)
  8. at org.apache.commons.httpclient.HttpMethodBase.readStatusLine(HttpMethodBase.java:1973)
  9. 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的超时用法小记的更多相关文章

  1. linux expect, spawn用法小记

    linux expect, spawn用法小记_IT民工_百度空间 linux expect, spawn用法小记 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://sys ...

  2. [转载]expect spawn、linux expect 用法小记

    原文地址:expect spawn.linux expect 用法小记作者:悟世 使用expect实现自动登录的脚本,网上有很多,可是都没有一个明白的说明,初学者一般都是照抄.收藏.可是为什么要这么写 ...

  3. HttpURLConnection和HttpClient的简单用法

    HttpURLConnection的简单用法:先通过一个URL创建一个conn对象,然后就是可以设置get或者是post方法,接着用流来读取响应结果即可 String html = null; lon ...

  4. Matlab norm 用法小记

    Matlab norm 用法小记 matlab norm (a) 用法以及实例 norm(A,p)当A是向量时norm(A,p)   Returns sum(abs(A).^p)^(1/p), for ...

  5. Shell常见用法小记

    shell的简单使用 最近发现shell脚本在平常工作中简直算一把瑞士军刀,很多场景下用shell脚本能实现常用的简单需求,而之前都没怎么学习过shell,就趁机把shell相关的语法和常见用法总结了 ...

  6. (五)HttpClient 连接超时及读取超时

    第一节: HttpClient 连接超时及读取超时 HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接 ...

  7. android httpclient 设置超时

    3.X是这样的 HttpClient httpClient=new DefaultHttpClient();4.3是这样的CloseableHttpClient httpClient = HttpCl ...

  8. HttpClient连接超时及读取超时

    HttpClient连接超时及读取超时 httpClient在执行具体http请求时候 有一个连接的时间和读取内容的时间: HttpClient连接时间 所谓连接的时候 是HttpClient发送请求 ...

  9. httpClient连接超时设置

    注: 每个HttpClinet对象设置都不一样 这里已3.x和4.x为例说明 1)3.X版本 创建连接 HttpClient httpClient=new DefaultHttpClient(); 这 ...

随机推荐

  1. 【风马一族_C】进制转化

    #include "stdio.h" #include "Math.h" #define number 50 //设置数组的长度 int num10; //十进 ...

  2. float闭合(清除浮动)和CSS HACK

    一.float 闭合(清除浮动) 将以下代码加入Global CSS 中,给需要闭合的div加上 class="clearfix" 即可,屡试不爽. <style>.c ...

  3. Flex-box 学习

    .flex-cont{ /*定义为flexbox的“父元素”*/ display: -webkit-box; display: -webkit-flex; display: flex; /*子元素沿主 ...

  4. 分享:linux下apache服务器的配置和管理

    linux下apache服务器的配置和管理. 一.两个重要目录: Apache有两个重要的目录:1.配置目录/etc/httpd/conf:2.文档目录/var/www: 二.两种配置模式: Apac ...

  5. 图片放大缩小(和ViewPager配合使用流畅显示)--第三方开源--PhotoView

    图片的放大缩小实现效果是使用的github上的一个开源项目photoView实现的,下载地址:https://github.com/chrisbanes/PhotoView 下面看测试代码: acti ...

  6. openerp经典收藏 对象的预定义方法(转载)

    对象的预定义方法 原文:http://shine-it.net/index.php/topic,2159.15.html 每个OpenERP的对象都有一些预定义方法,这些方法定义在基类osv.osv中 ...

  7. c#中的类型转换

    Parse类型转换 Parse()函数 int.double都能调用Parse()函数,Parse(string str);如果转换成功就成功,失败就会抛出一个异常; TryParse()函数 相应地 ...

  8. wpf MVVM ViewModel 关闭View显示

    上次说到了不同wpf窗体之间的交互,这个在MVVM模式之中用起来会方便很多 下面我来说下在ViewModel中关闭View的方法,其实也很简单的,注释照样不写,一看就懂的 public partial ...

  9. 1049. Counting Ones/整数中1出现的次数(从1到n整数中1出现的次数)

    The task is simple: given any positive integer N, you are supposed to count the total number of 1's ...

  10. 存储映射IO

    mmap 将文件映射到内存, 对这块内存的修改会自动同步到相应的文件中 void *mmap(void *addr, size_t len, int prot, int flag, int fd, o ...