网络超时设置connectionTimeout和SoTimeout的区别


就是为了记住这两个超时时间的区别:

简单总结为:连接时间超时connectionTimeout和读取数据超时soTimeout

我使用的场景是在android的开源框架Xutils中使用的

http.configTimeout(30000);  连接超时     http.configSoTimeout(timeout)获取数据超时

  一:连接超时:connectionTimeout

 

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

 

     2:设置方法为:

	/**
	 * @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();
		}
	}
}
设置一个错误的url,l连接超时3000报错

二:读取数据超时:soTimeout

 

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

 

     2:设置方法

	/**
	 * @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();
		}
	}
}





  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值