Apache HttpComponents学习笔记(三):配置HttpClient
2016-10-26 10:18
225 查看
释放系统资源
只有关闭streamed Entity的content stream或/和关闭响应对象,才能释放系统资源。区别在于,关闭content stream能保持底层连接的存活,而关闭响应对象会立即关闭并丢弃底层连接。EntityUtils.consume(HttpEntity)方法能用来保证streamed Entity能正常关闭content stream.CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
// do something useful
} finally {
instream.close();
}
}
} finally {
response.close();
}
如果只关闭响应对象,不关闭streamed Entity的content stream, 也能正常释放系统资源,并且无法再从content stream中读取内容。这适用于只需要从content stream中读取部分内容就释放资源的场景。
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int byteOne = instream.read();
int byteTwo = instream.read();
// Do not need the rest
}
} finally {
response.close();
}
EntityUtils的toByteArray和toString方法可以用于从Entity里读取完整的内容到byte数组或字符串里。但是,不建议这么做,除非Entity来自可信的服务器并且内容长度有限。
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet("http://localhost/");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
HttpEntity entity = response.getEntity();
if (entity != null) {
long len = entity.getContentLength();
if (len != -1 && len < 2048) {
System.out.println(EntityUtils.toString(entity));
} else {
// Stream content out
}
}
} finally {
response.close();
}
使用ResponseHandler
CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("http://localhost/json");
ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() {
@Override
public JsonObject handleResponse(
final HttpResponse response) throws IOException {
StatusLine statusLine = response.getStatusLine();
HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= 300) {
throw new HttpResponseException(
statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
if (entity == null) {
throw new ClientProtocolException("Response contains no content");
}
Gson gson = new GsonBuilder().create();
ContentType contentType = ContentType.getOrDefault(entity);
Charset charset = contentType.getCharset();
Reader reader = new InputStreamReader(entity.getContent(), charset);
return gson.fromJson(reader, MyJsonObject.class);
}
};
MyJsonObject myjson = client.execute(httpget, rh);
配置Keep-Alive timeout
ConnectionKeepAliveStrategy keepAliveStrat = new DefaultConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration( HttpResponse response, HttpContext context) { long keepAlive = super.getKeepAliveDuration(response, context); if (keepAlive == -1) { // Keep connections alive 5 seconds if a keep-alive value // has not be explicitly set by the server keepAlive = 5000; } return keepAlive; } }; CloseableHttpClient httpclient = HttpClients.custom() .setKeepAliveStrategy(keepAliveStrat) .build();
配置HTTP protocol interceptors
CloseableHttpClient httpclient = HttpClients.custom() .addInterceptorLast(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { AtomicInteger count = (AtomicInteger) context.getAttribute("count"); request.addHeader("Count", Integer.toString(count.getAndIncrement())); } }) .build(); AtomicInteger count = new AtomicInteger(1); HttpClientContext localContext = HttpClientContext.create(); localContext.setAttribute("count", count); HttpGet httpget = new HttpGet("http://localhost/"); for (int i = 0; i < 10; i++) { CloseableHttpResponse response = httpclient.execute(httpget, localContext); try { HttpEntity entity = response.getEntity(); } finally { response.close(); } }
配置请求重试Handler
HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() { public boolean retryRequest( IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; CloseableHttpClient httpclient = HttpClients.custom() .setRetryHandler(myRetryHandler) .build();
配置重导向策略
LaxRedirectStrategy redirectStrategy = new LaxRedirectStrategy(); CloseableHttpClient httpclient = HttpClients.custom() .setRedirectStrategy(redirectStrategy) .build();
相关文章推荐
- Apache HTTP最新官方配置(中译版)
- Apache HTTP最新官方配置
- SVN-1.5.3+Apache-2.0.59在windows下的配置与权限设置,以及配置HTTP方式访问SVN服务器
- Apache HTTP Server 2.0 安装和配置记录
- window下Apache HTTP Server 2.2.4 配置代理解决ajax跨域问题
- Apache HTTP最新官方配置(中译版[1])
- windows下apache配置SSL以实现http转换为https
- Apache HTTP最新官方配置(中译版)
- APACHE2.0 TOMCAT5.0 SSL 配置 转帖(http://www.cnblogs.com/bjrmt/archive/2008/09/17/1292239.html)
- 配置Apache 2.2+PHP 5.2.9支持OCI通过Oracle9i Client连接Oracle
- Apache HTTP最新官方配置(中译版)
- 给Tomcat,Apache配置gzip压缩(HTTP压缩)功能
- 配置Apache 2.2+PHP 5.2.9支持OCI通过Oracle9i Client连接Oracle
- apache配置文件:http.conf配置详解
- Apache下配置WAP+OTA方法(摘自 http://www.wangzhansheji.com/2/article_75.html)
- Apache HTTP最新官方配置(中译版[4])
- Apache Common HttpClient使用之七种武器
- Apache Http server +tomcat 实现域名重写配置
- 配置Apache Http Server2.2.4与Tomcat6.014