---
title: "httpclient4 注意事项"
author: "fenglin"
date: "2011-11-01T23:19:38Z"
tags: []
categories: ["编程语言"]
slug: "httpclient4"
draft: false
lastmod: "2011-11-01T23:19:38Z"
id: 1228233
url: "https://fenglin.iteye.com/blog/1228233"
---

httpclient4 相对于httpclient3变化很大

 

里面涉及到碰到的几点 记录一下

 

 

1、ClientConnManager 的超时设置

 

 

```
httpClient.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 30 * 1000);
httpClient.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 30 * 1000);
```

 

2、当设置了以上超时 运行多线程的时候 进入调试子线程 会发现很多的Socket 处于read状态

     这里需要Get Post 超时设置

 

 

```
httpGet.getParams().setIntParameter(HttpConnectionParams.CONNECTION_TIMEOUT, 30 * 1000);
httpGet.getParams().setIntParameter(HttpConnectionParams.SO_TIMEOUT, 30 * 1000);
```

 

 

3、*ConnectionPoolTimeout 连接池超时*

 

 

```
HttpEntity entity = response.getEntity();

if (entity != null) {
	buffer = new StringBuffer(EntityUtils.toString(entity));
}

//  必须加入释放
EntityUtils.consume(entity);
```