原设置如下:
CloseableHttpClient httpclient = HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new HttpPost(url);
String result = null;
try {
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(xmlStr.getBytes()));
reqEntity.setContentType("application/xml");
reqEntity.setContentEncoding("utf-8");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
logger.info("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
if (response != null &&
response.getStatusLine() != null &&
response.getStatusLine().getStatusCode() != 200) {
logger.error("server code:{}", response.getStatusLine().getStatusCode());
logger.error("server text:{}", response);
}
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "GB2312");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
logger.error("httpPost-ClientProtocolException", e);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
logger.error("httpPost-UnsupportedEncodingException", e1);
} catch (IOException e) {
e.printStackTrace();
logger.error("httpPost-IOException", e);
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("httpPost-IOException", e);
}
}
要加超时时间,修改后为:
// 创建默认的httpClient实例.
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
// 创建httppost
HttpPost httppost = new HttpPost(url);
String result = null;
try {
InputStreamEntity reqEntity = new InputStreamEntity(new ByteArrayInputStream(xmlStr.getBytes()));
reqEntity.setContentType("application/xml");
reqEntity.setContentEncoding("utf-8");
reqEntity.setChunked(true);
httppost.setEntity(reqEntity);
logger.info("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
if (response != null &&
response.getStatusLine() != null &&
response.getStatusLine().getStatusCode() != 200) {
logger.error("server code:{}", response.getStatusLine().getStatusCode());
logger.error("server text:{}", response);
}
HttpEntity entity = response.getEntity();
result = EntityUtils.toString(entity, "GB2312");
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
logger.error("httpPost-ClientProtocolException", e);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
logger.error("httpPost-UnsupportedEncodingException", e1);
} catch (IOException e) {
e.printStackTrace();
logger.error("httpPost-IOException", e);
} finally {
// 关闭连接,释放资源
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("httpPost-IOException", e);
}
}
主要的改变是初始化httpclient时候:
// 创建默认的httpClient实例.
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
以上是修改了http请求的超时时间。