简体   繁体   中英

apache HttpClient: Invalid use of SingleClientConnManager

I am getting java.lang.IllegalStateException: Invalid use of SingleClientConnManager: connection still allocated. 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. Do I need to make an explicit call EntityUtils.consume() as well?

You should probably utilize an EntityUtils.consume() to consume any remaining content in the stream if you have not already done so.

Or take @dnault's suggestion and close the stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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