From 18efaffed46fd3215d0176ede92367b9287ec11a Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Sun, 16 Mar 2025 22:17:29 -0700 Subject: [PATCH] Make sure the connection is always disconnected --- src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt b/src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt index 2575d04..f34c992 100644 --- a/src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt +++ b/src/main/kotlin/net/thauvin/erik/isgd/Isgd.kt @@ -51,16 +51,20 @@ class Isgd private constructor() { companion object { private fun callApi(url: String): String { val connection = URL(url).openConnection() as HttpURLConnection - connection.setRequestProperty( - "User-Agent", - "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109.0" - ) - if (connection.responseCode in 200..399) { - return connection.inputStream.bufferedReader().use { it.readText() } - } else { - throw IsgdException( - connection.responseCode, - connection.errorStream.bufferedReader().use { it.readText() }) + try { + connection.setRequestProperty( + "User-Agent", + "Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109.0" + ) + if (connection.responseCode in 200..399) { + return connection.inputStream.bufferedReader().use { it.readText() } + } else { + throw IsgdException( + connection.responseCode, + connection.errorStream.bufferedReader().use { it.readText() }) + } + } finally { + connection.disconnect() } }