An OkHttp request does not have a general mutable proxy field. Choose the client with the intended route before creating its call.

When the rest of the configuration should stay shared, derive the routed client from an existing client:

OkHttpClient routed = baseClient.newBuilder()
    .proxy(new Proxy(Proxy.Type.HTTP,
        new InetSocketAddress("203.0.113.10", 8080)))
    .build();

The endpoint is illustrative. Public HTTP endpoints from the HTTP proxy list can fill this socket address for an allowed public-service route, provided the base client's policy supplies no proxy credentials to the new endpoint. OkHttp documents that newBuilder shares connection-pool and thread resources with the original client. That does not mean a socket established through one proxy can be relabelled as another route. Connection eligibility still depends on the route and address configuration.

For repeated work, retain a bounded set of clients keyed by the routing policy you actually need. Avoid an unbounded map whose keys contain every temporary account or proxy variation ever observed.

Resource sharing also affects shutdown. Closing a shared cache or shutting down a shared dispatcher can affect other derived clients using those resources. Give resource ownership a clear application-level lifetime rather than treating every derived client as an isolated network stack.

Finally, include the selected proxy identity in the call's own metadata. A global “last proxy chosen” variable cannot identify the route of several concurrent requests.

Tënë sô zo