@@ -146,25 +146,23 @@ private static TxOut txOutFromMap(Map<String, Object> map) {
146146
147147 private static String readString (URI uri ) {
148148 HttpURLConnection connection ;
149- StringBuilder buffer = new StringBuilder ();
149+ StringBuilder inputBuffer = new StringBuilder ();
150150 try {
151151 connection = (HttpURLConnection ) uri .toURL ().openConnection ();
152152 int httpStatus = connection .getResponseCode ();
153153 if (httpStatus != HttpURLConnection .HTTP_OK ) throw new IOException ("Unexpected HTTP status: " + httpStatus );
154154 try (InputStream inputStream = connection .getInputStream ()) {
155155 if (inputStream == null ) throw new IOException ("No input stream" );
156- if (connection .getInputStream () != null ) {
157- BufferedReader in = new BufferedReader (new InputStreamReader (connection .getInputStream ()));
158- String inputLine ;
159- while ((inputLine = in .readLine ()) != null ) buffer .append (inputLine );
160- in .close ();
161- }
156+ BufferedReader in = new BufferedReader (new InputStreamReader (inputStream ));
157+ String inputLine ;
158+ while ((inputLine = in .readLine ()) != null ) inputBuffer .append (inputLine );
159+ in .close ();
162160 }
163161 } catch (IOException ex ) {
164162 throw new RuntimeException ("Cannot read from " + uri + "; " + ex .getMessage (), ex );
165163 }
166- if (log .isDebugEnabled ()) log .debug ("Read response from " + uri + ": " + buffer );
167- return buffer .toString ();
164+ if (log .isDebugEnabled ()) log .debug ("Read response from " + uri + ": " + inputBuffer );
165+ return inputBuffer .toString ();
168166 }
169167
170168 private static Map <String , Object > readObject (URI uri ) {
@@ -194,7 +192,18 @@ private static void writeBytes(URI uri, byte[] bytes) {
194192 outputStream .write (bytesHex , 0 , bytesHex .length );
195193 }
196194 int httpStatus = connection .getResponseCode ();
197- if (httpStatus != HttpURLConnection .HTTP_OK ) throw new IOException ("Unexpected HTTP status: " + httpStatus );
195+ if (httpStatus != HttpURLConnection .HTTP_OK ) {
196+ StringBuilder errorBuffer = new StringBuilder ();
197+ try (InputStream errorStream = connection .getErrorStream ()) {
198+ if (errorStream != null ) {
199+ BufferedReader in = new BufferedReader (new InputStreamReader (errorStream ));
200+ String inputLine ;
201+ while ((inputLine = in .readLine ()) != null ) errorBuffer .append (inputLine );
202+ in .close ();
203+ }
204+ }
205+ throw new IOException ("Unexpected HTTP status: " + httpStatus + " (" + errorBuffer + ")" );
206+ }
198207 connection .disconnect ();
199208 } catch (IOException ex ) {
200209 throw new RuntimeException ("Cannot read from " + uri + "; " + ex .getMessage (), ex );
0 commit comments