全部产品
Search
文档中心

阿里云SDK:代理配置

更新时间:Sep 20, 2022

升级版本 SDK 同样也可以通过环境变量:

  • HTTP_PROXY 或者http_proxy

  • HTTPS_PROXY

  • NO_PROXY

支持代理或者通过 Config 在初始化 Client 阶段设置请求的代理可以设置,升级版 SDK 还支持通过运行时参数配置当前请求的代理优先级:RuntimeOption -> Client > 环境变量:

public static void Main(string[] args)
{
    AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
    {
        // 您的AccessKey ID
        AccessKeyId = accessKeyId,
        // 您的AccessKey Secret
        AccessKeySecret = accessKeySecret,
    };
    // 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);

    // 运行时参数设置,仅对使用了该运行时参数实例的请求有效
    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();
    // 通过 SDK Core 执行实际的 Http 请求将返回的结果序列化为 SDK 的 Response 结构
    var response = client.DescribeInstancesWithOptions(request, runtimeOptions);
    System.Console.WriteLine(response.Body.TotalCount);
}