Kotlin makes server-side development concise yet powerful. Here is a basic HTTP proxy server in Kotlin in less than 20 lines of code.
First we import the required packages:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.http.*
import io.ktor.client.*
import io.ktor.client.request.*
The ktor server package provides the API for the proxy server, ktor client to make requests, and ktor http for dealing with HTTP connections.
Next we create an embedded server:
embeddedServer(Netty, port = 8080) {
// routing code
}.start(wait = true)
Inside the routing configuration we handle GET requests:
get("/") {
// proxy logic
}
To proxy the request we:
val url = call.request.path().substring(1)
val response = client.get<String>(url)
call.respond(response)
Here is the full HTTP proxy in Kotlin:
import io.ktor.server.engine.*
import io.ktor.server.netty.*
// other imports
fun main() {
embeddedServer(Netty, port = 8080) {
get("/") {
val url = call.request.path().substring(1)
val response = client.get<String>(url)
call.respond(response)
}
}.start(wait = true)
}
This shows how concise and expressive an HTTP proxy can be in Kotlin using its coroutines and ktor library.
This is great as a learning exercise but it is easy to see that even the proxy server itself is prone to get blocked as it uses a single IP. In this scenario where you may want a proxy that handles thousands of fetches every day using a professional rotating proxy service to rotate IPs is almost a must.
Otherwise, you tend to get IP blocked a lot by automatic location, usage, and bot detection algorithms.
Our rotating proxy server Proxies API provides a simple API that can solve all IP Blocking problems instantly.
Hundreds of our customers have successfully solved the headache of IP blocks with a simple API.
The whole thing can be accessed by a simple API like below in any programming language.
In fact, you don't even have to take the pain of loading Puppeteer as we render Javascript behind the scenes and you can just get the data and parse it any language like Node, Puppeteer or PHP or using any framework like Scrapy or Nutch. In all these cases you can just call the URL with render support like so:
curl "<http://api.proxiesapi.com/?key=API_KEY&render=true&url=https://example.com>"
We have a running offer of 1000 API calls completely free. Register and get your free API Key.