THRIFT-1211. java: When using THttpClient, non 200 responses leave the connection open
This patch reorders the code so that we can close the connection in the case of an error.
Patch: Mathias Herberts
git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1137794 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/java/src/org/apache/thrift/transport/THttpClient.java b/lib/java/src/org/apache/thrift/transport/THttpClient.java
index 550d76d..b16da68 100644
--- a/lib/java/src/org/apache/thrift/transport/THttpClient.java
+++ b/lib/java/src/org/apache/thrift/transport/THttpClient.java
@@ -233,6 +233,13 @@
HttpResponse response = this.client.execute(this.host, post);
int responseCode = response.getStatusLine().getStatusCode();
+
+ //
+ // Retrieve the inputstream BEFORE checking the status code so
+ // resources get freed in the finally clause.
+ //
+
+ is = response.getEntity().getContent();
if (responseCode != HttpStatus.SC_OK) {
throw new TTransportException("HTTP Response code: " + responseCode);
@@ -245,8 +252,6 @@
// Proceeding differently might lead to exhaustion of connections and thus
// to app failure.
- is = response.getEntity().getContent();
-
byte[] buf = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();