就是为了记住这两个超时时间的区别:
简单总结为:连接时间超时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();
}
}
}