HttpClient有个很坑的地方,这个玩意发送请求,没设置超时时间,只要不响应,他能一直在这等着,如果是线程池中某个线程在执行这个东西,可想而知时间久了,线程池就会满,导致其他事项不能正常执行,下面就说下如何添加超时时间。
HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(10);
httpConnectionManager.getParams().setConnectionTimeout(10000);
httpConnectionManager.getParams().setSoTimeout(10000);
HttpClient client = new HttpClient(httpConnectionManager);
PostMethod method = new PostMethod("url地址");
method.addParameter("p0", "");
client.executeMethod(method);
//打印服务器返回的状态
System.out.println("服务器返回状态" + method.getStatusLine());
//打印服务器返回的数据
String returnJson = method.getResponseBodyAsString();
System.out.println(returnJson);