All Products
Search
Document Center

Alibaba Cloud SDK:Configure a proxy

Last Updated:Jun 19, 2023

You can configure a proxy for Alibaba Cloud Darabonba SDK for .NET by using the following environment variables:

  • HTTP_PROXY or http_proxy

  • HTTPS_PROXY

  • NO_PROXY

You can configure a proxy when you initialize an SDK client in the Darabonba SDK. You can also configure a proxy by using RuntimeOptions. The priority levels of the proxies that can be configured are listed in descending order: the proxy that you configure by using RuntimeOptions, the proxy that you configure when you initialize an SDK client, and the proxy that you configure by using environment variables.

public static void Main(string[] args)
{
    AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
    {
        // Your AccessKey ID.
        AccessKeyId = accessKeyId,
        // Your AccessKey secret.
        AccessKeySecret = accessKeySecret,
    };
    // Configure a proxy when you initialize an SDK client.
    config.HttpProxy = "http://127.0.0.1:9898";
    config.HttpsProxy = "http://user:password@127.0.0.1:8989";
    config.NoProxy = "127.0.0.1,localhost";

    config.Endpoint = "ecs-cn-hangzhou.aliyuncs.com";
    AlibabaCloud.SDK.Ecs20140526.Client client = new AlibabaCloud.SDK.Ecs20140526.Client(config);

    // Configure a proxy by using runtime parameters. The setting takes 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";


    DescribeInstancesRequest request = new DescribeInstancesRequest();
    // Process HTTP requests to serialize the return result into a response object in the SDK based on the core library.
    var response = client.DescribeInstancesWithOptions(request, runtimeOptions);
    System.Console.WriteLine(response.Body.TotalCount);
}