All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Dec 13, 2024

This topic describes how to configure a proxy in Alibaba Cloud SDK V2.0 for .NET

Proxy configuration methods

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.

    using AlibabaCloud.TeaUtil.Models;
    using Tea;
    
    namespace demo
    {
        public class Sample
        {
            static void Main(string[] args)
            {
                var ecsConfig = new AlibabaCloud.OpenApiClient.Models.Config
                {
                    // Obtain the AccessKey ID from an environment variable. 
                    AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                    // Obtain the AccessKey secret from an environment variable. 
                    AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                    Endpoint = "ecs.cn-beijing.aliyuncs.com"
                };
    
                var escClient = new AlibabaCloud.SDK.Ecs20140526.Client(ecsConfig);
                var describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest();
                // Use RuntimeOptions to configure timeout periods. The setting takes effect only on the requests that use the RuntimeOptions object.
                var 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";
    
                var response = escClient.DescribeRegionsWithOptions(describeInstancesRequest, runtime);
                Console.WriteLine(response.Body.ToMap());
            }
        }
    }
  • The following code provides an example on how to configure a proxy by using the Config object when you initialize an SDK client.

    using AlibabaCloud.TeaUtil.Models;
    using Tea;
    
    namespace demo
    {
        public class Sample
        {
            static void Main(string[] args)
            {
                var ecsConfig = new AlibabaCloud.OpenApiClient.Models.Config
                {
                    // Obtain the AccessKey ID from an environment variable. 
                    AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"),
                    // Obtain the AccessKey secret from an environment variable. 
                    AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
                    Endpoint = "ecs.cn-beijing.aliyuncs.com",
                    // Specify the proxy parameters. The proxy parameter settings take effect for SDK clients that are initialized by using the Config.
                    NoProxy = "127.0.0.1,localhost",
                    HttpProxy = "http://127.0.0.1:9898",
                    HttpsProxy = "http://user:password@127.0.0.1:8989"
                };
    
                var escClient = new AlibabaCloud.SDK.Ecs20140526.Client(ecsConfig);
                var describeInstancesRequest = new AlibabaCloud.SDK.Ecs20140526.Models.DescribeRegionsRequest();
                var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
    
                var response = escClient.DescribeRegionsWithOptions(describeInstancesRequest, runtime);
                Console.WriteLine(response.Body.ToMap());
            }
        }
    }