Httpclienthandler Webproxy 值没有改变

Httpclienthandler Webproxy value isn't changing

有人可以帮助我吗?我在将布尔值 属性 设置为 true 时遇到了一些问题。

我在这里为我的代理创建了一个 class,这样我就可以设置并从我的 winform 调用我的 httpclient class。

class MyProxy
{
    public static bool UseProxy { get; set; }
    public static string ProxyHost { get; set; } = string.Empty; 
    public static string ProxyPort { get; set; } = string.Empty;
}

如果我想使用代理,这里是可选的

private static HttpClientHandler handler = new HttpClientHandler
{
    UseDefaultCredentials = false,
    PreAuthenticate = true,
    Proxy = new WebProxy($"http://{MyProxy.ProxyHost}:{MyProxy.ProxyPort}", false),
    UseProxy = MyProxy.UseProxy, // i want to change this if I want to use the proxy or not
};

所以在我的主窗体中,我有一个复选框是否使用代理

private void TEST()
{
  bool isCheck = cbUseProxy.Checked;
  
  //so here I'll check first if isCheck is true
  MyProxy.UseProxy = isCheck;
  
  //here I call my httpclient class
}

问题是,如果我先设置或先选中复选框(即 useProxy),Webproxy 就会获取该值。我取消选中(这不是使用代理)webproxy 仍然获得第一个值(这是真的)。​​

我想将 UseProxy(来自 WebProxy)的值从 true 更改为 false,将 false 更改为 true。

更新的最终解决方案:(感谢@Milney 的建议。我认为这是解决方案)

private static HttpClientHandler handler = new HttpClientHandler
        {
            PreAuthenticate = true,
            UseDefaultCredentials = false,
            Proxy = null,
            UseProxy = false,
        };
        private static HttpClientHandler handlerWithProxy = new HttpClientHandler
        {
            PreAuthenticate = true,
            UseDefaultCredentials = false,
            Proxy = new WebProxy($"http://{MyProxy.ProxyHost}:{MyProxy.ProxyPort}", false),
            UseProxy = true,
        };

        private static readonly HttpClient client = new HttpClient(handler);
        private static readonly HttpClient clientWithProxy = new HttpClient(handlerWithProxy);

然后在我的 ResponseMessage 上

     private static async Task<JsonDocument> ResponseMessage(HttpRequestMessage request, CancellationToken token)
        {
            HttpCompletionOption option = HttpCompletionOption.ResponseHeadersRead;
            bool useProxy = MyProxy.UseProxy;

            using (var response = useProxy ? await clientWithProxy.SendAsync(request, option, token).ConfigureAwait(false) 
                                           : await client.SendAsync(request, option, token).ConfigureAwait(false))
            {
                token.ThrowIfCancellationRequested();

                using (var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
                {
                    var json = await ParseJsonFromStream(contentStream);

                    if (!response.IsSuccessStatusCode)
                    {
                        if (json != null)
                        {
                            throw new InvalidDataException($"Error occured: {ParseError(uri, json)}");
                        }
                    }
                    return json;
                }
            }
        }

在您创建 HttpClientHandler 时,MyProxy.UseProxy 具有其默认值。 如果您稍后更改它,它不会自动 'flow' 到客户端处理程序属性。您需要重新创建 HttpClientHandler,或者至少在您实际获得要使用的值后重置 属性...

private void TEST()
{
  bool isCheck = cbUseProxy.Checked;
  
  // You have to set the property on the handler
  // It won't automatically refresh from your other class
  handler.UseProxy = isCheck;   

  //here I call my httpclient class
}