Ensured HttpUrlConnection is properly disconnected

This commit is contained in:
Erik C. Thauvin 2024-11-27 22:26:46 -08:00
parent fcbfe63aee
commit f27501634c
Signed by: erik
GPG key ID: 776702A6A2DA330E

View file

@ -421,14 +421,22 @@ object Utils {
@Throws(IOException::class) @Throws(IOException::class)
fun URL.reader(): UrlReaderResponse { fun URL.reader(): UrlReaderResponse {
val connection = this.openConnection() as HttpURLConnection val connection = this.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"
return if (connection.responseCode.isHttpSuccess()) { )
UrlReaderResponse(connection.responseCode, connection.inputStream.bufferedReader().use { it.readText() }) return if (connection.responseCode.isHttpSuccess()) {
} else { UrlReaderResponse(
UrlReaderResponse(connection.responseCode, connection.errorStream.bufferedReader().use { it.readText() }) connection.responseCode,
connection.inputStream.bufferedReader().use { it.readText() })
} else {
UrlReaderResponse(
connection.responseCode,
connection.errorStream.bufferedReader().use { it.readText() })
}
} finally {
connection.disconnect()
} }
} }