各位大侠好,最近碰到一个问题,在使用httpclient的post方法时,遇到了超时错误,如下
169318787-12-16 13:57:36 - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out
169318899:12-16 13:57:36 - Retrying request
根据httpclient的默认策略,是会重试三次,但是三次以后就直接抛出了ConnectException这个异常,网上说超时异常是可以通过IOException来捕获的,但是这里实际上该异常没有被捕获,求解,程序如下
public static  byte[] getBody(String url, String parms) {
byte[] body = null;
// 构造HttpClient的实例
HttpClient httpClient = new HttpClient();
// 创建GET方法的实例
PostMethod postMethod = new PostMethod(url);
postMethod.setRequestHeader("Content-type", "text/xml; charset=GBK");
// 将表单的值放入postMethod中
try {
postMethod.setRequestEntity(new StringRequestEntity(parms,
"text/xml; charset=GBK", null));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 执行postMethod
int statusCode;
try {
statusCode = httpClient.executeMethod(postMethod); // HttpClient对于要求接受后继服务的请求,象POST和PUT等不能自动处理转发
// 301或者302
if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY
|| statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
// 从头中取出转向的地址
Header locationHeader = postMethod
.getResponseHeader("location");
String location = null;
if (locationHeader != null) {
location = locationHeader.getValue();
System.out
.println("The page was redirected to:" + location);
} else {
System.err.println("Location field value is null.");
}
}
body = postMethod.getResponseBody();
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}  finally {
// 释放连接
postMethod.releaseConnection();
}
return body;
}