一:连接超时:connectionTimeout
1:指的是连接一个url的连接等待时间。
二:读取数据超时:soTimeout
1:指的是连接上一个url,获取response的返回等待时间。
For example:
// 设置连接时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.getHttpConnectionManager().getParams().setSoTimeout(60000);
/**
* 消息发送处理.
* HTTP发送方式
* @param content 消息内容
* @return 返回消息
*/
private GeneralReturnInfo postHttpData(String content) {
GeneralReturnInfo out = new GeneralReturnInfo();
String result = "";
PostMethod postMethod = new PostMethod(serviceUrl); try {
postMethod.setParameter("content", content); HttpClient client = new HttpClient();
client.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
// 设置连接时间
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
client.getHttpConnectionManager().getParams().setSoTimeout(60000);
int status = client.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
result = postMethod.getResponseBodyAsString();
if (StringUtils.isEmpty(result)) {
String msg="HTTP访问失败(返回报文为空)(" + serviceUrl + ").";
System.err.println(msg);
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, "", msg);
} out = JSON.parseObject(result, GeneralReturnInfo.class);
} else {
String msg="HTTP访问:返回状态不等于200(" + status + ")(" + serviceUrl + ")).";
System.err.println(msg);
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, "", msg);
} } catch (Exception e) { // 将新产生的例外封装
if (e instanceof AdapterException) {
throw (AdapterException) e;
} else if (e instanceof ConnectException) {
System.err.println("HTTP访问失败(连接失败)(" + serviceUrl + ")).");
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(连接失败)(" + serviceUrl + ")).");
} else if (e instanceof ConnectTimeoutException) {
System.err.println("HTTP访问失败(连接超时)(" + serviceUrl + ")).");
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(连接超时)(" + serviceUrl + ")).");
} else if (e instanceof SocketTimeoutException) {
System.err.println("HTTP访问失败(访问超时)(" + serviceUrl + ")).");
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(访问超时)(" + serviceUrl + ")).");
} else {
System.err.println("HTTP访问失败(调用异常)(" + serviceUrl + ")).");
throw new AdapterException(ErrorType.CLIENT_NET_ERROR, e, "HTTP访问失败(调用异常)(" + serviceUrl + ")).");
}
} finally {
// 释放连接
postMethod.releaseConnection();
}
return out;
}
com.creditharmony.apporveadapter.core.client.ClientPoxy 第170行;