diff --git a/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java b/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java index 9042a55c7..bad60626e 100644 --- a/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java +++ b/google-http-client/src/main/java/com/google/api/client/http/javanet/NetHttpRequest.java @@ -75,10 +75,21 @@ public LowLevelHttpResponse execute() throws IOException { connection.setChunkedStreamingMode(0); } OutputStream out = connection.getOutputStream(); + boolean threw = true; try { getStreamingContent().writeTo(out); + threw = false; } finally { - out.close(); + try { + out.close(); + } catch (IOException exception) { + // When writeTo() throws an exception, chances are that the close call will also fail. + // In such case, swallow exception from close call so that the underlying cause can + // propagate. + if (!threw) { + throw exception; + } + } } } else { // cannot call setDoOutput(true) because it would change a GET method to POST