各位大侠好,最近碰到一个问题,在使用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;
}
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;
}
解决方案 »
- Dispatcher initialization failed 请高手帮忙
- 怎么样学习Java
- ubuntu9.10 tomcat指定jdk
- 我现在做的系统出现了重复i定位的问题,是一个关于arcGIS在java上的应用
- application中文显示小方块?中文问题,乱码问题。
- 高手们,说明一下什么是WebLogic。有什么用。如何使用,安装和配置。
- 调用webservice结果传回的资料不全……
- 使用hibernate遇到的问题
- Spring mvc静态资源加载不到
- 请问用java开发个什么样的项目最好
- spring管理hibernate事务(CSDN的朋友们帮个忙)
- 这段信息什么意思?我使用jstl标签时出错。
一般连接超时看看防火墙,看看网络。
// TODO Auto-generated catch block
e.printStackTrace();
} 把杀毒软件防火墙关了看看