HttpClient 连接要设置超时
2015-05-05 14:50
211 查看
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();
}
}
}
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();
}
}
}
相关文章推荐
- httpclient连接超时时间设置
- Httpclient 设置读取超时及连接重试等配置
- httpclient4.5以上设置连接超时的…
- HttpClient连接请求超时设置
- HttpClient 4.5版本设置连接超时时间
- HttpClient 4.5版本设置连接超时时间
- python下设置urllib连接超时
- C#中设置Socket连接请求的超时
- 【c#】设置Socket连接、接收超时(转)
- 连接数据库超时设置autoReconnect=true
- socket测试远程地址能否连接并为连接设置超时
- 蓝牙连接断开超时时间设置
- android网络编程注意事项之一:移动网络下,防止网络超时甚至连接不上,解决办法--为网络请求设置代理
- ORA-03135 防火墙超时设置断开db link 连接
- 配置远程桌面服务会话的超时设置和重新连接设置
- ExtJs 中 Ajax 连接超时设置与问题排查
- xfire设置连接超时问题
- Linux设置socket连接超时
- httpClient请求超时的设置以及处理
- 上传文件,时间超时 Apache 如何设置保持连接