简体   繁体   English

WebProxy - 使用 C# 代码或机器进行配置

[英]WebProxy - use C# code or machine to configure

I am writing a .NET (C#) windows application.我正在编写一个 .NET (C#) windows 应用程序。 I have an HttpClientHandler for which I setup a WebProxy.我有一个 HttpClientHandler,我为其设置了一个 WebProxy。

I don't know if is it better to set no proxy and use the machine proxy settings?不知道是不是不设置代理,用机器代理设置好? or set up the HttpClient to use a WebProxy inside the code.或设置 HttpClient 以在代码中使用 WebProxy。 In both cases, the result would be the same - the request will use the same proxy.在这两种情况下,结果都是一样的——请求将使用相同的代理。

Is there a more reliable or "industry accepted" choice between the two?两者之间是否有更可靠或更“行业公认”的选择?

What happens if both the machine settings sets up the proxy, and the code does it too?如果两个机器设置都设置了代理,并且代码也设置了代理,会发生什么?

ADDITIONAL NOTE - both choices are mine to make.附加说明 - 这两个选择都是我自己做出的。 I do not depend on a customer for any of those choices.我不依赖客户做出任何这些选择。 The question is pure in the sense of reliability as I found that with machine set WebProxy settings, every so often my clients failed to go trough.这个问题在可靠性方面是纯粹的,因为我发现使用机器设置的 WebProxy 设置,我的客户端经常无法通过。

I just recently went through a very similar issue as this while dealing with the WebProxy for the first time.我最近在第一次处理 WebProxy 时遇到了与此非常相似的问题。

I have a HttpClientHandler that sets up my web proxy as well.我有一个 HttpClientHandler 也可以设置我的 Web 代理。 My team and I chose to handle this in code because it was easier to test and verify via unit tests.我和我的团队选择在代码中处理这个问题,因为通过单元测试更容易测试和验证。

To make this configurable, we have the app read in the configuration details from a secure configuration that is set at install time.为了使其可配置,我们让应用程序从安装时设置的安全配置中读取配置详细信息。 If the proxy configuration changes this can be updated in the config file.如果代理配置更改,则可以在配置文件中更新。 This also works for us because our tool is a data aggregation tool that sends to a very specific web API.这也适用于我们,因为我们的工具是一个数据聚合工具,可以发送到非常特定的 Web API。

I used the WebProxy with an HttpClientHandler with my HttpClient to do all of this.我使用带有 HttpClientHandler 的 WebProxy 和我的 HttpClient 来完成所有这些。

Here are some links that helped me out: MSDN Solution .Net WebProxy doc以下是一些帮助我解决问题的链接: MSDN Solution .Net WebProxy doc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM