天天看点

HttpClient的超时用法小记

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

   测试版本为HttpClient——3.1

      一:连接超时:connectionTimeout

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

      2:设置方法为:

       public class TestHttpClientMain {

       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

       二:读取数据超时:soTimeout

      1:指的是连接上一个url,获取response的返回等待时间

      2:设置方法

      public class TestHttpClientMain {

    3:测试的时候的连接url为我本地开启的一个url,http://localhost:8080 /firstTest.htm?method=test

      在我这个测试url里,当访问到这个链接时,线程sleep一段时间,来模拟返回response超时。

       @RequestMapping(params = "method=test") //<——②

      4:将读取response返回超时时间设的时间比那个sleep时间短之后,运行程序给出异常:

        java.net.SocketTimeoutException: Read timed out

ok,以后再写httpClient这两个超时时间一定要加上,不加就很可能悲剧的了。