This topic describes how to configure a proxy in the V1.0 SDK.
Proxy types
You can use HTTP and HTTPS proxies. An HTTP proxy is used for HTTP requests, and an HTTPS proxy is used for HTTPS requests. After you configure a proxy, you can also configure the SDK to bypass the proxy for specific Alibaba Cloud products that do not require proxy access.
Configuration methods
The SDK provides two methods to configure a proxy:
Use environment variables:
HTTP_PROXYorhttp_proxy: Specifies the address of the HTTP proxy server.HTTPS_PROXY: Specifies the address of the HTTPS proxy server.NO_PROXY: Specifies a list of addresses to bypass the proxy. Separate multiple addresses with commas. Domain names and IP addresses are supported.
Configure in your code: Use the
com.aliyuncs.http.HttpClientConfigclass to configure the proxy. This class provides the following methods:setHttpProxy(): Specifies the address of the HTTP proxy server.
setHttpsProxy(): Specifies the address of the HTTPS proxy server.
setNoProxy(): Specifies a list of addresses to bypass the proxy. Separate multiple addresses with commas. Domain names and IP addresses are supported.
public static void main(String[] args) { com.aliyuncs.http.HttpClientConfig clientConfig = com.aliyuncs.http.HttpClientConfig.getDefault(); // Set the HTTPS proxy. You must also set the ProtocolType. clientConfig.setHttpsProxy("http://user:password@127.0.0.1:8989"); clientConfig.setProtocolType(com.aliyuncs.http.ProtocolType.HTTPS); // Set the HTTP proxy. clientConfig.setHttpProxy("http://127.0.0.1:9898"); // Set the list of addresses to bypass the proxy. clientConfig.setNoProxy("ecs-cn-hangzhou.aliyuncs.com,dysmsapi.aliyuncs.com"); com.aliyuncs.profile.DefaultProfile profile = com.aliyuncs.profile.DefaultProfile.getProfile( "<REGION_ID>", // Obtain the AccessKey ID of the RAM user from an environment variable. System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), // Obtain the AccessKey secret of the RAM user from an environment variable. System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET") ); profile.setHttpClientConfig(clientConfig); com.aliyuncs.IAcsClient client = new com.aliyuncs.DefaultAcsClient(profile); // The code for calling the API is omitted. }
References
For more information, see HTTP Proxy Configuration Best Practices.