Read HTTP error stream before closing it

It wasn't causing problems, but it looks odd.
This commit is contained in:
Ivan Vilata-i-Balaguer 2016-04-07 11:46:24 +02:00
parent 51059fe6a9
commit e6197938fe

View file

@ -22,14 +22,15 @@ public class HttpClient {
URL url = new URL(baseUrl + param); URL url = new URL(baseUrl + param);
connection = (HttpURLConnection) url.openConnection(); connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET"); connection.setRequestMethod("GET");
connection.setConnectTimeout(10000); connection.setConnectTimeout(10_000);
connection.setReadTimeout(10000); connection.setReadTimeout(10_000);
if (connection.getResponseCode() == 200) { if (connection.getResponseCode() == 200) {
return convertInputStreamToString(connection.getInputStream()); return convertInputStreamToString(connection.getInputStream());
} else { } else {
String error = convertInputStreamToString(connection.getErrorStream());
connection.getErrorStream().close(); connection.getErrorStream().close();
throw new HttpException(convertInputStreamToString(connection.getErrorStream())); throw new HttpException(error);
} }
} finally { } finally {
if (connection != null) if (connection != null)