实践HttpClient

HttpClient

PostMethod.setHeader(key,value)

body

PostMethod.setEntity(HttpEntity)

HttpEntity

HTTP protocol parameters

PostMethod.setParams(HttpParams)

connection manager

关闭相应的io对象即可,会自动回收连接到连接池。

@since 4.0

ClientConnectionManager有两个自带的实现PoolingClientConnectionManagerBasicClientConnectionManager

1
2
3
4
5
ClientConnectionManager ccm = new PoolingClientConnectionManager();
((PoolingClientConnectionManager) ccm).setMaxTotal(10000);
((PoolingClientConnectionManager) ccm).setDefaultMaxPerRoute(300);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm);
HttpConnectionParams.setConnectionTimeout(httpclient.getParams(),10*1000);

@since 4.3

HttpClientConnectionManager有两个自带的实现PoolingHttpClientConnectionManagerBasicHttpClientConnectionManager

1
2
3
4
5
6
7
8
9
10
11
12
13
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager();
poolingHttpClientConnectionManager.setMaxTotal(10000);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(300);

RequestConfig requestConfig = RequestConfig.custom()
	.setConnectionRequestTimeout(CONNECTIONREQUEST_TIMEOUT)
	.setConnectTimeout(CONNECT_TIMEOUT)
	.setSocketTimeout(SOCKET_TIMEOUT).build();

httpClient = HttpClients.custom()
	.setConnectionManager(poolingHttpClientConnectionManager)
	.setDefaultRequestConfig(requestConfig)
	.build();

Core

Client

AsyncClient