简体   繁体   English

异常--- SingleClientConnManager:仍然分配连接

[英]Exception---SingleClientConnManager: connection still allocated

I have a code to read the response of a page. 我有一个代码来读取页面的响应。 it looks like this. 它看起来像这样。

public static void main(String[] args)
    {
        DefaultHttpClient httpclient = new DefaultHttpClient();
        HttpEntity entity  = null;
        try{

            String it= "https://www.aaa/login";
                HttpPost httpost = new HttpPost(it);

                List <NameValuePair> ln= new ArrayList <NameValuePair>();
                ln.add(new BasicNameValuePair("un", "aaa"));
                ln.add(new BasicNameValuePair("pwd", "bbb"));
                httpost.setEntity(new UrlEncodedFormEntity(ln, HTTP.UTF_8));

                HttpResponse  response = httpclient.execute(httpost);
                entity  = response.getEntity();
                GZIPInputStream gis= new GZIPInputStream(entity.getContent());
            StringBuffer st = new StringBuffer();
                for (int c = gis.read(); c != -1; c = gis.read()) {
                    st.append((char)c);
                }

                    CookieStore cookieStore = httpclient.getCookieStore();
                    HttpContext httpContext = new BasicHttpContext();
                    httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
                    HttpGet httpget = new HttpGet("http://aaa/index");
                    HttpResponse httpResponse = httpclient.execute(httpget, httpContext);
                    entity = httpResponse.getEntity();

                    if (entity != null) {
                        BufferedReader br = new BufferedReader(new InputStreamReader(entity.getContent()));
                        String readLine;
                        while(((readLine = br.readLine()) != null)) {
                          System.err.println("br   :"+readLine);
                    }


                }                
        }catch(Exception e){
            e.printStackTrace();
        }
        finally{

            EntityUtils.consume(entity);
            httpclient.getConnectionManager().shutdown();
        }

When I run this program i get the foll exception. 当我运行这个程序时,我得到了foll异常。

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:216)
    at org.apache.http.impl.conn.SingleClientConnManager$1.getConnection(SingleClientConnManager.java:190)
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:401)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:820)
    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:754)

I have closed the connection in the finally block. 我已经关闭了finally块中的连接。 As suggested in aonther thread I have consumed the response body before reusing the connection. 正如在其他线程中所建议的那样,我在重用连接之前已经使用了响应主体。 what else would go wrong here? 还有什么会出错?

before 之前

HttpResponse httpResponse = httpclient.execute(httpget, httpContext);

you also need 你还需要

EntityUtils.consume(entity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无效使用SingleClientConnManager:仍分配连接 - Invalid use of SingleClientConnManager: connection still allocated Android无效使用SingleClientConnManager:仍分配连接 - Android Invalid use of SingleClientConnManager: connection still allocated 使用HttpRequest.execute()的异常:无效使用SingleClientConnManager:仍分配了连接 - Exception using HttpRequest.execute(): Invalid use of SingleClientConnManager: connection still allocated BasicClientConnManager的使用无效:仍然分配了连接 - Invalid use of BasicClientConnManager: connection still allocated Resteasy客户端在方法抛出异常后保持分配连接 - Resteasy Client keeping connection allocated after a method throws an Exception JAVA EWS item.load throws请求失败。 仍然分配连接 - JAVA EWS item.load throws The request failed. Connection is still allocated 上传到Box的多个文件失败,HTTP客户端错误“仍然分配了连接” - Multiple files upload to Box fails with HTTP client error “connection still allocated” RabbitMQ 正在运行,但连接被拒绝连接异常 - RabbitMQ is running still getting connection refused Connect Exception 事务仍处于活动状态时无法关闭连接 connection.close() 上的异常 - Cannot close a connection while a transaction is still alive Exception on connection.close() 使用SingleClientConnManager和AsyncTask时出错 - Error using SingleClientConnManager and AsyncTask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM