@@ -95,32 +95,33 @@ public void fetch(HttpUrl url) throws IOException {
95
95
Request request = new Request .Builder ()
96
96
.url (url )
97
97
.build ();
98
- Response response = client .newCall (request ).execute ();
99
- String responseSource = response .networkResponse () != null
100
- ? ("(network: " + response .networkResponse ().code () + " over " + response .protocol () + ")" )
101
- : "(cache)" ;
102
- int responseCode = response .code ();
103
-
104
- System .out .printf ("%03d: %s %s%n" , responseCode , url , responseSource );
105
-
106
- String contentType = response .header ("Content-Type" );
107
- if (responseCode != 200 || contentType == null ) {
108
- response .body ().close ();
109
- return ;
110
- }
98
+ try (Response response = client .newCall (request ).execute ()) {
99
+ String responseSource = response .networkResponse () != null ? ("(network: "
100
+ + response .networkResponse ().code ()
101
+ + " over "
102
+ + response .protocol ()
103
+ + ")" ) : "(cache)" ;
104
+ int responseCode = response .code ();
105
+
106
+ System .out .printf ("%03d: %s %s%n" , responseCode , url , responseSource );
107
+
108
+ String contentType = response .header ("Content-Type" );
109
+ if (responseCode != 200 || contentType == null ) {
110
+ return ;
111
+ }
111
112
112
- MediaType mediaType = MediaType .parse (contentType );
113
- if (mediaType == null || !mediaType .subtype ().equalsIgnoreCase ("html" )) {
114
- response .body ().close ();
115
- return ;
116
- }
113
+ MediaType mediaType = MediaType .parse (contentType );
114
+ if (mediaType == null || !mediaType .subtype ().equalsIgnoreCase ("html" )) {
115
+ return ;
116
+ }
117
117
118
- Document document = Jsoup .parse (response .body ().string (), url .toString ());
119
- for (Element element : document .select ("a[href]" )) {
120
- String href = element .attr ("href" );
121
- HttpUrl link = response .request ().url ().resolve (href );
122
- if (link == null ) continue ; // URL is either invalid or its scheme isn't http/https.
123
- queue .add (link .newBuilder ().fragment (null ).build ());
118
+ Document document = Jsoup .parse (response .body ().string (), url .toString ());
119
+ for (Element element : document .select ("a[href]" )) {
120
+ String href = element .attr ("href" );
121
+ HttpUrl link = response .request ().url ().resolve (href );
122
+ if (link == null ) continue ; // URL is either invalid or its scheme isn't http/https.
123
+ queue .add (link .newBuilder ().fragment (null ).build ());
124
+ }
124
125
}
125
126
}
126
127
0 commit comments