WebApiClient icon indicating copy to clipboard operation
WebApiClient copied to clipboard

如何在WebApiClientCore下实现动态代理功能

Open JamLee0813 opened this issue 3 years ago • 1 comments

WebApiClient.JIT版本实现如下:

/// <summary>
///     使用设置代理(本地)
/// </summary>
public class LocalProxy : ApiActionFilterAttribute
{
    public override Task OnBeginRequestAsync(ApiActionContext context)
    {
        var webProxy = new WebProxy("127.0.0.1", 8888);
        context.HttpApiConfig.HttpHandler.UseProxy = true;
        context.HttpApiConfig.HttpHandler.Proxy = webProxy;

        return base.OnBeginRequestAsync(context);
    }
}

其中127.0.0.1和8888,我想通过调用静态属性去改变其代理,CORE版本应该如何实现,而不是一开始注册后就固定了,如下:

services.AddHttpApi<IBaiDu>().ConfigurePrimaryHttpMessageHandler(() => new HttpClientHandler {UseProxy = true, Proxy = new WebProxy("127.0.0.1", 8888)});

JamLee0813 avatar Dec 20 '21 06:12 JamLee0813

代理本质上是一种特殊的连接,CORE版本使用了HttpClientFactory,所以不支持代理切换。

xljiulang avatar Dec 28 '21 08:12 xljiulang

你可以用WebRequest.DefaultWebProxy去获取系统代理

ldqk avatar Jan 09 '23 08:01 ldqk