From ed415f692f22800e2db5be7a46226450cce94725 Mon Sep 17 00:00:00 2001 From: Inderjeet Singh Date: Thu, 30 Sep 2010 12:28:11 +0000 Subject: [PATCH] Ensuring that the HTTP connection is closed when the rest call is finished. --- .../com/google/gson/webservice/client/RestClient.java | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/wsclient/src/main/java/com/google/gson/webservice/client/RestClient.java b/wsclient/src/main/java/com/google/gson/webservice/client/RestClient.java index 0951e74e..c276ea52 100644 --- a/wsclient/src/main/java/com/google/gson/webservice/client/RestClient.java +++ b/wsclient/src/main/java/com/google/gson/webservice/client/RestClient.java @@ -65,12 +65,13 @@ public class RestClient { public RestResponse getResponse( RestCallSpec callSpec, RestRequest request, Gson gson) { + HttpURLConnection conn = null; try { URL webServiceUrl = getWebServiceUrl(callSpec); if (logger != null) { logger.log(logLevel, "Opening connection to " + webServiceUrl); } - HttpURLConnection conn = (HttpURLConnection) webServiceUrl.openConnection(); + conn = (HttpURLConnection) webServiceUrl.openConnection(); RestRequestSender requestSender = new RestRequestSender(gson, logLevel); requestSender.send(conn, request); RestResponseReceiver responseReceiver = @@ -80,9 +81,17 @@ public class RestClient { throw new WebServiceSystemException(e); } catch (IllegalArgumentException e) { throw new WebServiceSystemException(e); + } finally { + closeIgnoringErrors(conn); } } + private static void closeIgnoringErrors(HttpURLConnection conn) { + if (conn != null) { + conn.disconnect(); + } + } + @Override public String toString() { return String.format("config:%s", config);