Index: test/org/apache/commons/httpclient/TestResponseHeaders.java =================================================================== --- test/org/apache/commons/httpclient/TestResponseHeaders.java (revision 227236) +++ test/org/apache/commons/httpclient/TestResponseHeaders.java (working copy) @@ -363,4 +363,45 @@ method.shouldCloseConnection(connectionManager.getConection())); assertFalse("Connection should NOT be closed", method.isConnectionCloseForced()); } + + public void testNoContent() throws Exception { + // test with connection header + this.server.setRequestHandler(new HttpRequestHandler() { + public boolean processRequest(SimpleHttpServerConnection conn, + SimpleRequest request) throws IOException { + ResponseWriter out = conn.getWriter(); + out.println("HTTP/1.1 204 NO CONTENT"); + out.println(); + out.flush(); + return true; + } + }); + + GetMethod method = new GetMethod("/"); + client.executeMethod(method); + method.getResponseBodyAsString(); + + assertTrue(connectionManager.getConection().isOpen()); + + // test without connection header + this.server.setRequestHandler(new HttpRequestHandler() { + public boolean processRequest(SimpleHttpServerConnection conn, + SimpleRequest request) throws IOException { + ResponseWriter out = conn.getWriter(); + out.println("HTTP/1.1 204 NO CONTENT"); + out.println("Connection: keep-alive"); + out.println(); + out.flush(); + return true; + } + }); + + // test with connection header + method = new GetMethod("/"); + client.executeMethod(method); + method.getResponseBodyAsString(); + + assertTrue(connectionManager.getConection().isOpen()); + } + } Index: java/org/apache/commons/httpclient/HttpMethodBase.java =================================================================== --- java/org/apache/commons/httpclient/HttpMethodBase.java (revision 227236) +++ java/org/apache/commons/httpclient/HttpMethodBase.java (working copy) @@ -1677,6 +1677,7 @@ if (Wire.CONTENT_WIRE.enabled()) { is = new WireLogInputStream(is, Wire.CONTENT_WIRE); } + boolean canHaveBody = canResponseHaveBody(statusLine.getStatusCode()); InputStream result = null; Header transferEncodingHeader = responseHeaders.getFirstHeader("Transfer-Encoding"); // We use Transfer-Encoding if present and ignore Content-Length. @@ -1715,16 +1716,17 @@ } else { long expectedLength = getResponseContentLength(); if (expectedLength == -1) { - Header connectionHeader = responseHeaders.getFirstHeader("Connection"); - String connectionDirective = null; - if (connectionHeader != null) { - connectionDirective = connectionHeader.getValue(); + if (canHaveBody && this.effectiveVersion.greaterEquals(HttpVersion.HTTP_1_1)) { + Header connectionHeader = responseHeaders.getFirstHeader("Connection"); + String connectionDirective = null; + if (connectionHeader != null) { + connectionDirective = connectionHeader.getValue(); + } + if (!"close".equalsIgnoreCase(connectionDirective)) { + LOG.info("Response content length is not known"); + setConnectionCloseForced(true); + } } - if (this.effectiveVersion.greaterEquals(HttpVersion.HTTP_1_1) && - !"close".equalsIgnoreCase(connectionDirective)) { - LOG.info("Response content length is not known"); - setConnectionCloseForced(true); - } result = is; } else { result = new ContentLengthInputStream(is, expectedLength); @@ -1732,7 +1734,7 @@ } // See if the response is supposed to have a response body - if (!canResponseHaveBody(statusLine.getStatusCode())) { + if (!canHaveBody) { result = null; } // if there is a result - ALWAYS wrap it in an observer which will