55
66import java .io .IOException ;
77import java .io .InputStream ;
8+ import java .io .OutputStream ;
89import java .net .HttpURLConnection ;
910import java .net .URI ;
1011import java .net .URISyntaxException ;
2223public class WebClient8Impl extends WebClientCommon {
2324
2425 static class Header {
25- private String name ;
26- private String value ;
26+ private final String name ;
27+ private final String value ;
2728
2829 Header (String name , String value ) {
2930 this .name = name ;
@@ -116,18 +117,19 @@ public Java8WebRequestWithBody(String method, String url, String body) {
116117 public void completeRequest (HttpURLConnection connection ) throws IOException {
117118 super .completeRequest (connection );
118119 connection .setDoOutput (true );
119- connection .getOutputStream ().write (body .getBytes ());
120+
121+ try (final OutputStream outputStream = connection .getOutputStream ()) {
122+ outputStream .write (body .getBytes ());
123+ }
120124 }
121125 }
122126
123127 static class Java8WebResponse implements WebResponse {
124- private final WebRequest request ;
125128 private final HttpURLConnection connection ;
126129 private final int responseCode ;
127130 private final Map <String , List <String >> headerFields ;
128131
129- Java8WebResponse (WebRequest request , HttpURLConnection connection ) throws IOException {
130- this .request = request ;
132+ Java8WebResponse (HttpURLConnection connection ) throws IOException {
131133 this .connection = connection ;
132134 responseCode = connection .getResponseCode ();
133135 headerFields = connection .getHeaderFields ();
@@ -153,7 +155,7 @@ public Stream<String> getHeadersAsStream(String headerName) {
153155 }
154156
155157 @ Override
156- public void close () throws IOException {
158+ public void close () {
157159
158160 }
159161 }
@@ -164,7 +166,7 @@ public WebResponse send(WebRequest request) throws IOException {
164166 HttpURLConnection connection = openConnection (request .getURI ().toURL ());
165167 ((Java8WebRequest ) request ).completeRequest (connection );
166168
167- return new Java8WebResponse (request , connection );
169+ return new Java8WebResponse (connection );
168170 }
169171
170172 /**
@@ -186,7 +188,7 @@ private HttpURLConnection openConnection(URL url ) throws IOException {
186188 }
187189
188190 @ Override
189- public void close () throws IOException {
191+ public void close () {
190192
191193 }
192194 }
0 commit comments