Twilio Java SDK 13: Configure a Proxy with Apache HttpClient 5
Configure a proxy-aware NetworkHttpClient and pass it into TwilioRestClient without rebuilding the SDK request layer.
Twilio Java 13.1.0 accepts an Apache HttpClient 5 builder in NetworkHttpClient. Configure that builder's proxy, then pass the transport into the SDK client:
import com.twilio.http.NetworkHttpClient;
import com.twilio.http.TwilioRestClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.core5.http.HttpHost;
var apacheBuilder = HttpClientBuilder.create()
.setProxy(new HttpHost("http", "proxy.example", 8080));
var transport = new NetworkHttpClient(apacheBuilder);
var twilioClient = new TwilioRestClient.Builder(accountSid, authToken)
.httpClient(transport)
.build();
accountSid and authToken are the application's Twilio credentials. The fragment constructs the client; use it in the resource operation's client-accepting overload, such as create(twilioClient).
The http scheme describes the proxy connection. Twilio's API URL remains HTTPS. For an allowed public API test with no proxy account, an HTTP address from the HTTP proxy list can fill the builder’s proxy host and port.
For an authenticated proxy, attach an Apache credentials provider scoped to the proxy host and port before constructing NetworkHttpClient. Its account is separate from the Twilio API account.
Match the Apache client generation
Earlier Twilio releases used Apache 4 classes under org.apache.http. Apache 5 uses org.apache.hc packages and different HttpHost constructor signatures. Check the NetworkHttpClient constructor in the application's selected Twilio version before adapting this example.