Proxy

note

Proxy - 图1

This help topic is in development and will be updated in the future.

Ktor HTTP client allows using proxy in multiplatform code. The following document describes how to configure a proxy in ktor.

Multiplatform configuration

Create proxy

You don’t need to include additional artifacts to create a proxy. The supported proxy types are specific to a client engine. Two types of proxy can be configured in multiplatform: HTTP and SOCKS.

To create a proxy configuration use builders in the ProxyBuilder factory:

  1. // Create http proxy
  2. val httpProxy = ProxyBuilder.http("http://my-proxy-server-url.com/")
  3. // Create socks proxy
  4. val socksProxy = ProxyBuilder.socks(host = "127.0.0.1", port = 4001)

Proxy authentication and authorization are engine specific and should be handled by the user manually.

Set proxy

Proxy can be configured in multiplatform code using ProxyConfig builder in HttpClientEngineConfig block:

  1. val client = HttpClient() {
  2. engine {
  3. proxy = httpProxy
  4. }
  5. }

Platform-specific configuration

Jvm

The ProxyConfig class maps to Proxy class on the jvm:

  1. val httpProxy = Proxy(Proxy.Type.HTTP, InetSocketAddress(4040))

The most of Jvm client engines support it out of the box.

Note: Apache and CIO engines support HTTP proxy only. Jetty client engine doesn’t support any proxy.

Native

The native ProxyConfig class can use url to determine proxy address:

  1. val socksProxy = ProxyConfig(url = "socks://my-socks-proxy.com/")

Supported proxy types are engine specific. To see supported URLs consult with engine provider documentation:

Js

The proxy configuration is unsupported by platform restrictions.