İçeriğe geç
Guides 2 dk okuma 688 görüntülenme

Using Proxies in Kotlin

Proxy setup in Kotlin: OkHttp, Ktor, Java Proxy API, SOCKS5 and authentication for Android and JVM applications.

Using Proxies in Kotlin

Using Proxies in Kotlin

Proxies in Kotlin

Kotlin runs on the JVM and has full access to the Java API for working with proxies. Additionally, popular Kotlin libraries (OkHttp, Ktor) provide their own convenient APIs for configuring proxies.

OkHttp is the most popular HTTP client for Kotlin/Android.

HTTP Proxy

import okhttp3.OkHttpClient
import okhttp3.Request
import java.net.InetSocketAddress
import java.net.Proxy

val proxy = Proxy(
    Proxy.Type.HTTP,
    InetSocketAddress("proxy_ip", 8080)
)

val client = OkHttpClient.Builder()
    .proxy(proxy)
    .build()

val request = Request.Builder()
    .url("https://httpbin.org/ip")
    .build()

client.newCall(request).execute().use { response ->
    println(response.body?.string())
}

SOCKS5 Proxy

val proxy = Proxy(
    Proxy.Type.SOCKS,
    InetSocketAddress("proxy_ip", 1080)
)

val client = OkHttpClient.Builder()
    .proxy(proxy)
    .build()

Authentication

import okhttp3.Authenticator
import okhttp3.Credentials
import okhttp3.Route

val proxyAuthenticator = Authenticator { _: Route?, response: okhttp3.Response ->
    val credential = Credentials.basic("username", "password")
    response.request.newBuilder()
        .header("Proxy-Authorization", credential)
        .build()
}

val client = OkHttpClient.Builder()
    .proxy(proxy)
    .proxyAuthenticator(proxyAuthenticator)
    .build()

Ktor Client

Ktor is an asynchronous framework from JetBrains, native to Kotlin.

HTTP Proxy

import io.ktor.client.*
import io.ktor.client.engine.okhttp.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import java.net.InetSocketAddress
import java.net.Proxy

val client = HttpClient(OkHttp) {
    engine {
        proxy = Proxy(
            Proxy.Type.HTTP,
            InetSocketAddress("proxy_ip", 8080)
        )
    }
}

suspend fun main() {
    val response: HttpResponse = client.get("https://httpbin.org/ip")
    println(response.bodyAsText())
    client.close()
}

With CIO Engine

import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.request.*
import io.ktor.client.statement.*

val client = HttpClient(CIO) {
    engine {
        proxy = io.ktor.client.engine.ProxyBuilder.http("http://proxy_ip:8080")
    }
}

SOCKS via Ktor

val client = HttpClient(OkHttp) {
    engine {
        proxy = Proxy(
            Proxy.Type.SOCKS,
            InetSocketAddress("proxy_ip", 1080)
        )
    }
}

Java Proxy API

The standard Java API works in Kotlin:

import java.net.HttpURLConnection
import java.net.InetSocketAddress
import java.net.Proxy
import java.net.URL

val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("proxy_ip", 8080))
val url = URL("https://httpbin.org/ip")
val connection = url.openConnection(proxy) as HttpURLConnection

// Аутентификация
val encoded = java.util.Base64.getEncoder()
    .encodeToString("user:pass".toByteArray())
connection.setRequestProperty("Proxy-Authorization", "Basic $encoded")

connection.inputStream.bufferedReader().use { reader ->
    println(reader.readText())
}

Proxy Rotation

import okhttp3.OkHttpClient
import okhttp3.Request
import java.net.InetSocketAddress
import java.net.Proxy

data class ProxyConfig(val host: String, val port: Int, val user: String, val pass: String)

val proxies = listOf(
    ProxyConfig("proxy1", 8080, "user", "pass"),
    ProxyConfig("proxy2", 8080, "user", "pass"),
    ProxyConfig("proxy3", 8080, "user", "pass"),
)

fun getClient(proxyConfig: ProxyConfig): OkHttpClient {
    val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(proxyConfig.host, proxyConfig.port))
    return OkHttpClient.Builder()
        .proxy(proxy)
        .proxyAuthenticator { _, response ->
            val credential = okhttp3.Credentials.basic(proxyConfig.user, proxyConfig.pass)
            response.request.newBuilder()
                .header("Proxy-Authorization", credential)
                .build()
        }
        .build()
}

fun fetchWithRotation(url: String): String? {
    val proxy = proxies.random()
    val client = getClient(proxy)
    val request = Request.Builder().url(url).build()
    return try {
        client.newCall(request).execute().use { it.body?.string() }
    } catch (e: Exception) {
        println("Failed with ${proxy.host}: ${e.message}")
        null
    }
}

Android-Specific Considerations

Android System Proxies

On Android, you can use system proxies:

val proxyHost = System.getProperty("http.proxyHost")
val proxyPort = System.getProperty("http.proxyPort")?.toIntOrNull()

NetworkSecurityConfig

To work with proxies on Android 7+, you need to configure network_security_config.xml to allow cleartext traffic (if the proxy is HTTP).

Retrofit + Proxy

val proxy = Proxy(Proxy.Type.HTTP, InetSocketAddress("proxy_ip", 8080))
val okHttpClient = OkHttpClient.Builder().proxy(proxy).build()

val retrofit = Retrofit.Builder()
    .baseUrl("https://api.example.com/")
    .client(okHttpClient)
    .addConverterFactory(GsonConverterFactory.create())
    .build()

Timeouts and Settings

import java.util.concurrent.TimeUnit

val client = OkHttpClient.Builder()
    .proxy(proxy)
    .connectTimeout(10, TimeUnit.SECONDS)
    .readTimeout(30, TimeUnit.SECONDS)
    .writeTimeout(10, TimeUnit.SECONDS)
    .retryOnConnectionFailure(true)
    .build()

Conclusion

Kotlin offers several ways to work with proxies: OkHttp for simplicity and performance, Ktor for an asynchronous coroutine-based approach, and the standard Java API for compatibility. For Android development, OkHttp + Retrofit is the industry standard. All options support HTTP and SOCKS5 proxies with authentication.

Güncellendi: 06.03.2026
Kategoriye dön

Bunları da okuyun

Guides 2 dk

Nike SNKRS için Proxy: Sınırlı Sürümler ve Çoklu Katılım

Nike SNKRS'ta birden fazla hesapla çekilişlere katılmak, IP limitlerini aşmak ve ban yemeden sınırlı sürümleri kapmak için residential veya mobil proxy kullanın. Hangi türü seçmeli ve nasıl kurmalı.

Guides 2 dk

iPhone'da Proxy Nasıl Kurulur (Wi-Fi ve SOCKS5)

iPhone'da proxy'yi yerleşik Wi-Fi ayarlarından (HTTP/HTTPS) veya SOCKS5 ile hücresel kapsama için Shadowrocket gibi bir uygulamayla kurun. Adım adım iOS kurulumu ve bilinmesi gereken kısıtlar.

Guides 3 dk

Tinder için proxy: çoklu hesap ve ban'lerden kaçınma

Birden fazla profil işletmek ve IP shadowban'lerinden kaçınmak için Tinder'da mobil veya residential proxy kullanın. Mobilin neden kazandığı, kurulumu ve proxy'nin şehri neden değiştirmediği.

Guides 2 dk

StockX için proxy: fiyat takibi ve hesap yönetimi

StockX'te fiyatları izlemek, birden fazla hesap çalıştırmak ve drop'ları yasaklanmadan kapmak için residential, ISP veya mobil proxy kullanın. Hangi türü seçeceğinizi ve nasıl kuracağınızı anlatıyoruz.

Guides 3 dk

OpenAI API için Proxy: Erişim, Hız Limitleri ve Kurulum

OpenAI API'yi residential veya ISP proxy üzerinden yönlendirerek desteklenen bölgelere erişin, 429 hız limitlerinden kaçının ve hesapları izole edin. Hangi proxy'yi seçmeli ve Python kurulum örnekleri.

Guides 1 dk

E2E testleri için Cypress'te proxy kurulumu

Cypress'te proxy kurulumu: HTTP_PROXY değişkenleri, cy-proxy-middleware ve coğrafi konuma bağlı içeriğin test edilmesi.

Proxy'lerimizi deneyin

100+ ülkede 20,000+ proxy

support_agent
GProxy Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.