All Products
Search
Document Center

Alibaba Cloud SDK:Proxy configuration

Last Updated:Oct 15, 2025

In software development, a proxy is an intermediary that controls access to a feature or resource. You can use a proxy to implement features such as permission checks, operation log recording, data caching, and lazy loading without changing the original code. This makes your program more secure, improves its performance, and makes it easier to maintain and extend. This topic describes how to configure a proxy in the Java asynchronous software development kit (SDK).

Proxy types

The Java asynchronous SDK uses the com.aliyun.core.http.ProxyOptions class to configure the proxy type. The following two common proxy types are supported:

Type

Description

ProxyOptions.Type.HTTP

An HTTP proxy. It is suitable for most web network environments.

ProxyOptions.Type.SOCKS5

A SOCKS5 proxy. It is suitable for scenarios with complex network topologies.

Proxy configuration

1. Create a proxy address object

Create an InetSocketAddress object that represents the address of the proxy server:

java.net.InetSocketAddress inetSocketAddress = new java.net.InetSocketAddress("<YOUR-PROXY-HOSTNAME>", <port_number>);

Replace <YOUR-PROXY-HOSTNAME> with the actual hostname or IP address of the proxy server. Replace <port_number> with the actual port number.

2. Configure ProxyOptions

Create a ProxyOptions instance based on the proxy type:

// Example: Configure an HTTP proxy
com.aliyun.core.http.ProxyOptions proxyOptions = new com.aliyun.core.http.ProxyOptions(
    com.aliyun.core.http.ProxyOptions.Type.HTTP, 
    inetSocketAddress
);

// If the proxy requires authentication, set the username and password
proxyOptions.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>");

3. Apply the proxy configuration

After you configure the proxy, apply the configuration to the asynchronous client:

com.aliyun.core.http.HttpClient httpClient = new com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder()
        // Configure the proxy
        .proxy(proxyOptions)
        .build();
        
AsyncClient client = AsyncClient.builder()
        .credentialsProvider(credentialProvider)
        .httpClient(httpClient)
        .overrideConfiguration(
                ClientOverrideConfiguration.create()
                        .setEndpointOverride("<ENDPOINT>")
                        .setProtocol("https") // When using an HTTP proxy, set Protocol to http. For other proxies, use https.
        )
        .build();   

The following code provides a complete example:

// Configure the proxy IP address and port
java.net.InetSocketAddress inetSocketAddress = new java.net.InetSocketAddress("<YOUR-PROXY-HOSTNAME>", <port_number>);
com.aliyun.core.http.ProxyOptions proxyOptions = new com.aliyun.core.http.ProxyOptions(com.aliyun.core.http.ProxyOptions.Type.HTTP, inetSocketAddress);
// If the proxy requires authentication, set the username and password
proxyOptions.setCredentials("<YOUR-PROXY-USERNAME>", "<YOUR-PROXY-PASSWORD>");

com.aliyun.core.http.HttpClient httpClient = new com.aliyun.httpcomponent.httpclient.ApacheAsyncHttpClientBuilder()
        // Configure the proxy
        .proxy(proxyOptions)
        .build();

AsyncClient client = AsyncClient.builder()
        .credentialsProvider(credentialProvider) // The implementation of credentialProvider is omitted here
        .httpClient(httpClient)
        .overrideConfiguration(
                ClientOverrideConfiguration.create()
                        .setEndpointOverride("<ENDPOINT>")
                        .setProtocol("https") // Use https. If you use an HTTP proxy to make HTTP requests, set this parameter to http.
        )
        .build();

References

HTTP proxy configuration practice