本文为您介绍V2.0 .NET SDK对于代理的配置方式。
V2.0 SDK代理配置方式
说明
代理配置优先级:RuntimeOption配置->Client配置,优先级依次降低。
通过运行时参数(RuntimeOptions)配置当前请求的代理。
using AlibabaCloud.TeaUtil.Models; using Tea; namespace demo { public class Sample { static void Main(string[] args) { var ecsConfig = new AlibabaCloud.OpenApiClient.Models.Config { // 从环境变量中获取AccessKey ID值。 AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // 从环境变量中获取AccessKey Secret值。 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(); // 运行时参数设置,仅对使用了该运行时参数实例的请求有效 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()); } } }
通过Config在初始化Client阶段配置请求的代理。
using AlibabaCloud.TeaUtil.Models; using Tea; namespace demo { public class Sample { static void Main(string[] args) { var ecsConfig = new AlibabaCloud.OpenApiClient.Models.Config { // 从环境变量中获取AccessKey ID值。 AccessKeyId = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_ID"), // 从环境变量中获取AccessKey Secret值。 AccessKeySecret = Environment.GetEnvironmentVariable("ALIBABA_CLOUD_ACCESS_KEY_SECRET"), Endpoint = "ecs.cn-beijing.aliyuncs.com", // 代理参数设置,对使用该config的client都生效 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()); } } }