[转]HttpClient的超时用法小记

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

     测试版本为HttpClient——3.1

     一:连接超时:connectionTimeout

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

     2:设置方法为:

     public class TestHttpClientMain {

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

Java代码 复制代码 收藏代码
  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 {

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

Java代码 复制代码 收藏代码
  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

Java代码 复制代码 收藏代码
  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这两个超时时间一定要加上,不加就很可能悲剧的了。

posted @ 2013-07-30 09:03  奋斗?坑  阅读(574)  评论(0编辑  收藏  举报