HttpClient 连接要设置超时

private static void postWithJson(Map<String, String> map) {
String respMessage = "";
HttpClient httpClient = null;
PostMethod postMethod = null;


try {
int i = 0;
httpClient = new HttpClient();
postMethod = new PostMethod(DPS_LOGGER_PUBLIC_SERVICE_URL);
postMethod.getParams().setParameter(
HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
HttpConnectionManagerParams managerParams = httpClient 
   .getHttpConnectionManager().getParams(); 
 // 设置连接超时时间(单位毫秒) 
 managerParams.setConnectionTimeout(9000); 
 // 设置读数据超时时间(单位毫秒) 
 managerParams.setSoTimeout(12000); 
NameValuePair[] data = null;
if (map != null && map.size() > 0) {
data = new NameValuePair[map.size()];
for (Map.Entry<String, String> entry : map.entrySet()) {
data[i++] = new NameValuePair(entry.getKey(),
entry.getValue());
}
postMethod.setRequestBody(data);
}


int status = httpClient.executeMethod(postMethod);
if (status == HttpStatus.SC_OK) {
InputStream inputStream = postMethod.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
inputStream, "UTF-8"));
String str = "";
StringBuffer stringBuffer = new StringBuffer();
while ((str = br.readLine()) != null) {
stringBuffer.append(str);
}
respMessage = stringBuffer.toString();
} else {
String resultText = postMethod.getStatusLine().toString();
respMessage = "{\"resultCode\":10,\"resultText\":\""
+ resultText + "\",\"resultContent\":null}";
}


} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (postMethod != null) {
postMethod.releaseConnection();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值