简体   繁体   English

apache HttpClient:无效使用SingleClientConnManager

[英]apache HttpClient: Invalid use of SingleClientConnManager

I am getting java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated. 我正在获取java.lang.IllegalStateException:SingleClientConnManager的无效使用:连接仍被分配。 The code I am using is as follows: 我使用的代码如下:

        HttpResponse targetResponse = getHttpClient(true).execute(post,
                localContext);

        BufferedReader rd = new BufferedReader(new InputStreamReader(
                targetResponse.getEntity().getContent()));

        String line = new String();
        while ((line = rd.readLine()) != null) {
            if (!line.contains("attr1") && !line.contains("attr2"))
                continue;
            String[] splits = line.split(": ");
            if (splits[0].contains("attr1")) {
                attr1 = splits[1].trim();
            } else {
                attr2 = splits[1].trim();
            }

            if (attr1 != null && attr2 != null)
                break;
        }

My understanding is that as soon as I do the targetResponse.getEntity() the entity should be consumed. 我的理解是,一旦我执行targetResponse.getEntity(),就应该使用该实体。 Do I need to make an explicit call EntityUtils.consume() as well? 我是否还需要进行显式调用EntityUtils.consume()?

You should probably utilize an EntityUtils.consume() to consume any remaining content in the stream if you have not already done so. 如果尚未使用EntityUtils.consume()来消耗流中的所有剩余内容,则可能应该使用。

Or take @dnault's suggestion and close the stream. 或接受@dnault的建议并关闭流。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM