Proxy configuration
A proxy acts as an intermediary that controls access to a feature or resource, enabling permission checks, operation logging, data caching, and lazy loading without changing the original code. You can configure HTTP or SOCKS5 proxies in the Java asynchronous SDK to route API requests through a proxy server.
Proxy types
The Java asynchronous SDK uses the com.aliyun.core.http.ProxyOptions class to configure the proxy type. The following proxy types are supported:
|
Type |
Description |
|
|
An HTTP proxy, suitable for most web network environments. |
|
|
A SOCKS5 proxy, suitable for 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
Apply the proxy 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();
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();