Manually close okhttp responses. (#210)

Ensure response is closed properly.
This commit is contained in:
Kavin 2022-03-03 12:27:43 +00:00 committed by GitHub
parent cec135933e
commit 02ca78ee7a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 19 deletions

View file

@ -41,9 +41,12 @@ public class CaptchaSolver {
var builder = new Request.Builder().url(Constants.CAPTCHA_BASE_URL + "createTask")
.post(RequestBody.create(JsonWriter.string(jObject), MediaType.get("application/json")));
var resp = Constants.h2client.newCall(builder.build()).execute();
JsonObject taskObj = JsonParser.object()
.from(Constants.h2client.newCall(builder.build()).execute().body().byteStream());
.from(resp.body().byteStream());
resp.close();
return taskObj.getInt("taskId");
}
@ -62,9 +65,13 @@ public class CaptchaSolver {
.post(RequestBody.create(body, MediaType.get("application/json")));
builder.header("Content-Type", "application/json");
var resp = Constants.h2client.newCall(builder.build()).execute();
JsonObject captchaObj = JsonParser.object()
.from(Constants.h2client.newCall(builder.build()).execute().body().byteStream());
.from(resp.body().byteStream());
resp.close();
if (captchaObj.getInt("errorId") != 0)
break;

View file

@ -58,9 +58,9 @@ public class DownloaderImpl extends Downloader {
request.headers().forEach((name, values) -> values.forEach(value -> builder.header(name, value)));
var response = Constants.h2client.newCall(builder.build()).execute();
var resp = Constants.h2client.newCall(builder.build()).execute();
if (response.code() == 429) {
if (resp.code() == 429) {
synchronized (cookie_lock) {
@ -68,14 +68,14 @@ public class DownloaderImpl extends Downloader {
|| (System.currentTimeMillis() - cookie_received > TimeUnit.MINUTES.toMillis(30)))
saved_cookie = null;
String redir_url = String.valueOf(response.request().url());
String redir_url = String.valueOf(resp.request().url());
if (saved_cookie == null && redir_url.startsWith("https://www.google.com/sorry")) {
var formBuilder = new FormBody.Builder();
String sitekey = null, data_s = null;
for (Element el : Jsoup.parse(response.body().string()).selectFirst("form").children()) {
for (Element el : Jsoup.parse(resp.body().string()).selectFirst("form").children()) {
String name;
if (!(name = el.tagName()).equals("script")) {
if (name.equals("input"))
@ -118,7 +118,11 @@ public class DownloaderImpl extends Downloader {
}
return new Response(response.code(), response.message(), response.headers().toMultimap(), response.body().string(),
String.valueOf(response.request().url()));
var response = new Response(resp.code(), resp.message(), resp.headers().toMultimap(), resp.body().string(),
String.valueOf(resp.request().url()));
resp.close();
return response;
}
}

View file

@ -36,9 +36,13 @@ public class LbryHelper {
, MediaType.get("application/json")))
.build();
return new JSONObject(
Constants.h2client.newCall(request).execute().body().string()
).getJSONObject("result").getString("streaming_url");
var resp = Constants.h2client.newCall(request).execute();
var json = new JSONObject(resp.body().string());
resp.close();
return json.getJSONObject("result").getString("streaming_url");
}
}

View file

@ -14,7 +14,10 @@ public class RequestUtils {
public static String sendGet(String url, String ua) throws IOException {
var request = new Request.Builder().header("User-Agent", ua).url(url).build();
var response = Constants.h2client.newCall(request).execute();
var responseString = response.body().string();
response.close();
return Constants.h2client.newCall(request).execute().body().string();
return responseString;
}
}

View file

@ -1095,6 +1095,8 @@ public class ResponseHelper {
s.getTransaction().commit();
} else
System.out.println("Failed to subscribe: " + resp.code() + "\n" + Objects.requireNonNull(resp.body()).string());
resp.close();
}
}