Make sure the connection is always disconnected

This commit is contained in:
Erik C. Thauvin 2025-03-16 22:17:29 -07:00
parent 29a5448391
commit 0a843c06ec
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -51,16 +51,20 @@ class Isgd private constructor() {
companion object { companion object {
private fun callApi(url: String): String { private fun callApi(url: String): String {
val connection = URL(url).openConnection() as HttpURLConnection val connection = URL(url).openConnection() as HttpURLConnection
connection.setRequestProperty( try {
"User-Agent", connection.setRequestProperty(
"Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/109.0" "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() } if (connection.responseCode in 200..399) {
} else { return connection.inputStream.bufferedReader().use { it.readText() }
throw IsgdException( } else {
connection.responseCode, throw IsgdException(
connection.errorStream.bufferedReader().use { it.readText() }) connection.responseCode,
connection.errorStream.bufferedReader().use { it.readText() })
}
} finally {
connection.disconnect()
} }
} }