我有一个请求列表有10个请求,这些请求只是一个不同的参数格式,并且这些请求都发布到同一个URI。 我不太清楚releaseConnection是如何做的,我有一个如下代码:
HttpPost testPost = new HttpPost(http://test.mysite.com:8008/);
try{
for ( int index = 0; index < getRequests().size(); index++ ) {
String request = getRequests().get(i);
StringEntity se = new StringEntity(request , Consts.UTF_8);
se.setContentType("text/xml");
post.setEntity(se);
HttpResponse response = getHttpclient().execute(post);
SAXReader reader = new SAXReader();
Document document = reader.read(response.getEntity().getContent());
//do something with the document
}
} finally {
post.releaseConnection();
}
我尝试将循环修改为下面,它仍然有效:
HttpPost testPost = new HttpPost(http://test.mysite.com:8008/);
for ( int index = 0; index < getRequests().size(); index++ ) {
try{
String request = getRequests().get(i);
StringEntity se = new StringEntity(request , Consts.UTF_8);
se.setContentType("text/xml");
post.setEntity(se);
HttpResponse response = getHttpclient().execute(post);
SAXReader reader = new SAXReader();
Document document = reader.read(response.getEntity().getContent());
//do something with the document
} finally {
post.releaseConnection();
}
}
哪种方式更好地处理连接释放?
答案 0 :(得分:1)
你应该使用第一种方式,因为: