dynamically use proxy
parent
e2c13c2503
commit
24026d7fbe
|
|
@ -1,5 +1,6 @@
|
|||
package de.itkl.httpClient
|
||||
|
||||
import io.github.oshai.kotlinlogging.KotlinLogging
|
||||
import io.ktor.client.*
|
||||
import io.ktor.client.engine.*
|
||||
import io.ktor.client.engine.cio.*
|
||||
|
|
@ -7,8 +8,11 @@ import io.ktor.client.plugins.*
|
|||
import io.ktor.client.plugins.contentnegotiation.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.serialization.kotlinx.json.*
|
||||
import java.net.InetSocketAddress
|
||||
import java.net.Socket
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
|
||||
private val Log = KotlinLogging.logger { }
|
||||
fun createHttpClient(): HttpClient {
|
||||
return HttpClient(CIO) {
|
||||
install(ContentNegotiation) {
|
||||
|
|
@ -20,7 +24,15 @@ fun createHttpClient(): HttpClient {
|
|||
}
|
||||
|
||||
engine {
|
||||
proxy = ProxyBuilder.http(Url("http://localhost:9999"))
|
||||
val isPortOpen = try {
|
||||
Socket().use { it.connect(InetSocketAddress("localhost", 9999), 200) }
|
||||
true
|
||||
} catch (ex: Exception) {
|
||||
false
|
||||
}
|
||||
if (isPortOpen) {
|
||||
proxy = ProxyBuilder.http(Url("http://localhost:9999"))
|
||||
}
|
||||
https {
|
||||
trustManager = TrustAllX509TrustManager()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue