Additionally, the Response handling example on the website reccommends the use of a ResponseHandler and says, "The use of an HTTP response guarantees that the underlying HTTP connection will be released back to the connection manager automatically in all cases." The code also seems to violate this statement. This problem is "worked around" by just reading the entity manually in my code when I get an exception. However, I changed the library code to read the entity in a finally-block, which had the effect of fixing the connection relelase problem but also breaking the workaround. The workaround throws Exceptions of course because the connection and stream have been closed and the workaround attempts to read from them again.
-- David Hosier On Wednesday, October 5, 2011 at 1:44 PM, David Hosier wrote: > Perhaps I'm wrong, but the code for BasicResponseHandler in httpclient 4.1.2 > does not satisfy the javadocs as written. The javadoc states the following: > > "If the response code was >= 300, the response body is consumed and an > HttpResponseException > (http://hc.apache.org/httpcomponents-client-ga/httpclient/apidocs/org/apache/http/client/HttpResponseException.html) > is thrown." > > However, the code does not do that: > > StatusLine statusLine = response.getStatusLine(); > if (statusLine.getStatusCode() >= 300) { > throw new HttpResponseException(statusLine.getStatusCode(), > statusLine.getReasonPhrase()); > } > > HttpEntity entity = response.getEntity(); > return entity == null ? null : EntityUtils.toString(entity); > > The code clearly throws the Exception without reading the entity. So what > happens is that if you get a non-2xx response, connections are never released > as can be seen by enabling DEBUG logging for the library. Am I misreading the > code or javadocs, or is this really broken? If I catch the Exception and then > read the entity manually like shown above, I can see the connections being > closed. > > -David > > --------------------------------------------------------------------- > To unsubscribe, e-mail: [email protected] > (mailto:[email protected]) > For additional commands, e-mail: [email protected] > (mailto:[email protected]) --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
