diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java index 549ec91a86392700000144fd9eb81f9d42a0183b..5971945138d750271d27f6d731aef031ffddb558 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/util/http/HttpUtil.java @@ -70,6 +70,7 @@ public class HttpUtil { private HttpResponse doGetWithSerializeFunction(HttpRequest httpRequest, Function serializeFunction) { InputStreamReader isr = null; + InputStreamReader esr = null; int statusCode; try { HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection(); @@ -92,9 +93,18 @@ public class HttpUtil { conn.connect(); statusCode = conn.getResponseCode(); + try { + isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8); + } catch (Exception e) { + // ignore + } + try { + esr = new InputStreamReader(conn.getErrorStream(), StandardCharsets.UTF_8); + } catch (Exception e) { + // ignore + } if (statusCode == 200) { - isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8); String content = CharStreams.toString(isr); return new HttpResponse<>(statusCode, serializeFunction.apply(content)); } @@ -102,17 +112,26 @@ public class HttpUtil { if (statusCode == 304) { return new HttpResponse<>(statusCode, null); } - } catch (Throwable ex) { throw new ApolloConfigException("Could not complete get operation", ex); } finally { if (isr != null) { try { + CharStreams.toString(isr); isr.close(); } catch (IOException e) { // ignore } } + + if (esr != null) { + try { + CharStreams.toString(esr); + esr.close(); + } catch (Exception e) { + // ignore + } + } } throw new ApolloConfigStatusCodeException(statusCode,