This topic describes how to configure a proxy in Alibaba Cloud SDK V2.0 for Java.
Configuration method
Note
The proxy configurations take effect in the following descending order: the configuration by using the RuntimeOptions object and the configuration when you initialize an SDK client.
The following code provides an example on how to configure a proxy by using the RuntimeOptions object.
import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.models.DescribeRegionsRequest; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.models.RuntimeOptions; public class Sample { public static void main(String[] args) throws Exception { Config config = new Config(); // Obtain the AccessKey ID of the Resource Access Management (RAM) user from an environment variable. config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")); // Obtain the AccessKey secret of the RAM user from an environment variable. config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // Specify the region ID. config.setRegionId("<regionId>"); Client client = new Client(config); // Create a RuntimeOptions instance and specify runtime parameters. The settings take effect only on the requests that use the RuntimeOptions instance. RuntimeOptions runtimeOptions = new RuntimeOptions(); runtimeOptions.httpProxy = "http://127.0.0.1:9898"; runtimeOptions.httpsProxy = "http://user:password@127.0.0.1:8989"; runtimeOptions.noProxy = "127.0.0.1,localhost"; DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest(); client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions); } }
The following code provides an example on how to configure a proxy by using the Config object when you initialize an SDK client.
import com.aliyun.ecs20140526.Client; import com.aliyun.ecs20140526.models.DescribeRegionsRequest; import com.aliyun.teaopenapi.models.Config; import com.aliyun.teautil.models.RuntimeOptions; public class Sample { public static void main(String[] args) throws Exception { Config config = new Config(); // Obtain the AccessKey ID of the RAM user from an environment variable. config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID")); // Obtain the AccessKey secret of the RAM user from an environment variable. config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET")); // Specify the region ID. config.setRegionId("<regionId>"); // Configure a proxy when you initialize the client. config.setHttpProxy("http://127.0.0.1:9898"); config.setHttpsProxy("http://user:password@127.0.0.1:8989"); config.setNoProxy("127.0.0.1,localhost"); Client client = new Client(config); RuntimeOptions runtimeOptions = new RuntimeOptions(); DescribeRegionsRequest describeRegionsRequest = new DescribeRegionsRequest(); client.describeRegionsWithOptions(describeRegionsRequest, runtimeOptions); } }