`
ghost_fly
  • 浏览: 111401 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

Release Connection in Apache HttpComponents

    博客分类:
  • JAVA
阅读更多

原文:http://blog.blacklee.net/tech/707-release-connection-in-apache-httpcomponents

自从HttpClient升级到HttpComponent后,HttpMethod提供的方法releaseConnection()就被丢弃了。
转而改成另外相对麻烦点的维护一个连接池的处理方式。

When you replace Apache Commons HttpClient with Apache HttpComponents .
if you haven’t release connection in your codes, you may found exception traces like:

 

java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated.
Make sure to release the connection before allocating another one.
         at org.apache.http.impl.conn.SingleClientConnManager.getConnection(SingleClientConnManager.java:199)
         at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:173)
         at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:390)
         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:641)
         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:576)
         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:554)


But HttpComponents doesn’t provide method like void org.apache.commons.httpclient.HttpMethod.releaseConnection() .

It provides another method to do this work. And it’s more complex then before. Sample codes I wrote below:

 

public class Test {
    public static void main(String[] args) throws Exception {
        System.setProperty("org.apache.commons.logging.simplelog.log.httpclient.wire", "debug");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.httpclient", "debug");
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
        System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
        System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http", "debug");
 
        HttpParams httpParams = new BasicHttpParams();
        httpParams.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, 20);
        SchemeRegistry schreg = new SchemeRegistry();
        schreg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        schreg.register(new Scheme("https", PlainSocketFactory.getSocketFactory(), 443));
        ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(httpParams, schreg);
//        connManager.setMaxTotal(20);
 
        DefaultHttpClient client = new DefaultHttpClient(connManager, httpParams);
        String url = "http://www.google.com/";
        HttpGet get = new HttpGet(url);
        sendRequest(get, client);
        System.out.println("------------------2------------------");
        sendRequest(get, client);
        System.out.println("------------------3------------------");
        sendRequest(get, client);
        System.out.println("------------------4------------------");
        sendRequest(get, client);
        System.out.println("------------------5------------------");
        sendRequest(get, client);
    }
 
    private static void sendRequest(HttpGet get, DefaultHttpClient client) throws InterruptedException {
        HttpResponse resp = null;
        try {
            resp = client.execute(get);
            // get response content here
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                resp.getEntity().getContent().close(); //!!!IMPORTANT
            } catch (Exception e) {
                // go to hell
            }
        }
    }
}
 

Here are some notes:

  1. HC4.1 may deprecated some methods/properties.
  2. DON’T FORGET TO CLOSE THE RESPONSE STREAM


分享到:
评论

相关推荐

    使用apache httpcomponents 模拟html表单上传文件

    Apache HttpComponents 是一个强大的Java库,提供了全面的HTTP客户端支持,包括文件上传功能。这篇博客文章(链接:https://eof.iteye.com/blog/2153595)可能详细介绍了如何利用Apache HttpClient实现这个功能。 ...

    apache-httpcomponents-httpmime.jar

    apache-httpcomponents-httpmime.jar

    org.apache.http httpcomponents-client-4.5.3-bin.zip

    本篇将深入探讨`org.apache.http`组件,特别是`httpcomponents-client-4.5.3`版本在Java POST HTTP开发中的应用和关键知识点。 一、Apache HttpClient简介 Apache HttpClient 4.x系列是HttpClient项目的最新版本,...

    org.apache.http 需要的jar包,httpcomponents-client-4.5.10全部jar包

    commons-codec-1.11.jar commons-logging-1.2.jar fluent-hc-4.5.10.jar httpclient-4.5.10.jar httpclient-cache-4.5.10.jar httpclient-osgi-4.5.10.jar httpclient-win-4.5.10.jar ...jna-4.5.2.jar ...

    org.apache.http相关jar包下载(httpcomponents-client-4.2.5-bin.tar)

    在标题提到的`httpcomponents-client-4.2.5-bin.tar`中,我们找到了Apache HttpClient的特定版本4.2.5的二进制分发包,它通常包含了一系列的JAR文件,用于支持网络爬虫和其他需要与HTTP服务器交互的应用程序。...

    apache-httpcomponents-httpcore.jar.zip官网下的解压可用

    这个压缩包文件"apache-httpcomponents-httpcore.jar.zip"包含了HTTPCore组件,它是Apache HTTP Components项目的基础部分,专注于提供低级别的HTTP传输机制。在本文中,我们将深入探讨HTTPCore的基本概念、功能以及...

    Apache HttpComponents Client 4.4 二进制发布包

    鸿蒙HttpComponents Client 是一个高性能开源库,专为HTTP协议交互设计,广泛应用于Web服务、网络应用程序及不断扩展的云计算领域。尽管标准Java与.NET库具备基本HTTP通信能力,但它们的功能与灵活性往往无法满足...

    org.apache.httpcomponents.client4.11

    org.apache.httpcomponents.client4.11org.apache.httpcomponents.client4.11org.apache.httpcomponents.client4.11

    httpcomponents-client:Apache HttpClient的镜像

    Apache HttpComponents客户端欢迎使用Apache HttpComponents项目的HttpClient组件。建筑说明有关从源代码进行构建的信息,请参阅 。依存关系HttpClient主模块需要与Java 7兼容的运行时,并且依赖于以下外部库:其他...

    httpcomponents-client-4.5.6.rar

    标题中的"httpcomponents-client-4.5.6.rar"是一个压缩包文件,它是Apache HttpComponents项目的客户端组件的一个版本,具体是4.5.6版。HttpComponents是Java领域中用于处理HTTP协议的重要库,它提供了对HTTP协议的...

    httpclient-4.5.13-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpclient:4.5.13; 标签:apache、httpcomponents、httpclient、jar包、java、中文文档; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档...

    httpcore-nio-4.4.12-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpcore-nio:4.4.12; 标签:apache、httpcomponents、httpcore、nio、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览...

    httpcomponents

    本篇文章将深入探讨Apache HttpComponents的核心概念、功能以及在实际开发中的应用。 一、Apache HttpComponents概述 Apache HttpComponents是Apache软件基金会的一个项目,它提供了两个主要的模块:HttpClient和...

    httpclient-4.5.12-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpclient:4.5.12; 标签:apache、httpcomponents、httpclient、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档...

    httpcomponents-client-4.3.6

    Apache HttpComponents contains HttpClient and ... It has been replaced by the Apache HttpComponents project in its HttpClient and HttpCore modules, which offer better performance and more flexibility.

    httpcomponents-client-4.5.4-osgi-bin.tar.gz

    org.apache.httpcomponents.httpclient_4.5.4.jar 官方说明: The Apache HttpComponents™ project is responsible for creating and maintaining a toolset of low level Java components focused on ...

    apache-httpcomponents-httpclient-cache

    apache-httpcomponents-httpclient-cache

    httpcomponents-client-4.2.5

    HTTPComponents Client是Apache软件基金会开发的一个强大且灵活的HTTP客户端库,版本4.2.5是其历史中的一个重要里程碑。这个库为Java开发者提供了一套全面的工具,以处理HTTP请求和响应,实现了对HTTP协议的精细控制...

    httpcore-4.4.6-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpcore:4.4.6; 标签:apache、httpcomponents、httpcore、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 ...

    httpmime-4.5.13-API文档-中文版.zip

    Maven坐标:org.apache.httpcomponents:httpmime:4.5.13; 标签:apache、httpcomponents、httpmime、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。...

Global site tag (gtag.js) - Google Analytics