如何在 HttpClientHandler 上设置代理?

发布于 2025-01-11 08:43:39 字数 1527 浏览 0 评论 0原文

我已经尝试在我的 HttpClientHandler 上设置代理有一段时间了(将其传递给 HttpClient 来发出请求),但似乎没有任何效果,并且我的文档我正在阅读并没有多大帮助。

我遇到的主要错误是无法连接到代理,因为它们没有响应(它们是免费的,我在网上找到它们,但我尝试了几个网站,似乎很奇怪,它们都不适用于此)具体原因)。

这是我在 Program.cs 中的一些代码:

 private static async Task SetProxyAsync()
        {
            HttpClient httpClient = new HttpClient();
            var ip = await httpClient.GetStringAsync("https://api.ipify.org");
            Console.WriteLine($"My public IP address is: {ip}");

            HttpClientHandler handler = new HttpClientHandler();
            handler.UseProxy = true;
            handler.Proxy = new WebProxy("http://94.23.91.209:80/", true);

            HttpClient client2 = new HttpClient(handler);
            var ip2 = await client2.GetStringAsync("https://api.ipify.org");
            Console.WriteLine($"My proxy IP address is: {ip2}");
        }

我还有一个 Web.config 文件,但我不知道是否需要它/它对代码我已经在主程序中了。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <defaultProxy>
            <proxy usesystemdefault ="false"
            proxyaddress="http://94.23.91.209:80"
            bypassonlocal="false"/>
        </defaultProxy>
    </system.net>
</configuration>

我读了很多关于 WebProxyWebRequestHttpClient(Handler) 类的内容,但即使在阅读了很多内容之后,似乎也没有什么可以结合在一起的。文档以及这里和其他博客上的许多其他帖子,但我找不到任何可以回答我所遇到的问题的内容,所以首先感谢您阅读我的帖子,希望那里有人有答案:)

I've been trying to set a proxy on my HttpClientHandler for a while (to pass it on to a HttpClient to make requests) but nothing seems to work and the documentation I'm reading isn't helping much.

The main error I'm getting is about not being able to connect to the proxy because they're not responding (they're free, I found them online but I tried several websites and it seems odd that none of them would work for this specific reason).

Here's some of my code in Program.cs:

 private static async Task SetProxyAsync()
        {
            HttpClient httpClient = new HttpClient();
            var ip = await httpClient.GetStringAsync("https://api.ipify.org");
            Console.WriteLine(
quot;My public IP address is: {ip}");

            HttpClientHandler handler = new HttpClientHandler();
            handler.UseProxy = true;
            handler.Proxy = new WebProxy("http://94.23.91.209:80/", true);

            HttpClient client2 = new HttpClient(handler);
            var ip2 = await client2.GetStringAsync("https://api.ipify.org");
            Console.WriteLine(
quot;My proxy IP address is: {ip2}");
        }

I also have a Web.config file but I can't figure out if it's needed or not/what it changes to the code I have in the main program already.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.net>
        <defaultProxy>
            <proxy usesystemdefault ="false"
            proxyaddress="http://94.23.91.209:80"
            bypassonlocal="false"/>
        </defaultProxy>
    </system.net>
</configuration>

I read a bunch about the WebProxy, WebRequest and the HttpClient(Handler) classes but nothing seems to click together even after going through a lot of the documentation and a lot of other posts on here and other blogs, but I couldn't find anything that answered the kind of questions I have, so first of all thank you for reading my post and hopefully someone out there has an answer :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

表情可笑 2025-01-18 08:43:39

您的 XML 配置适用于 .NET Framework,但不适用于 .NET core。您可以将自己的 Proxy 对象传递给 HttpClientHandler (正如您已经尝试过的那样),或者需要使用环境变量。

https://learn.microsoft.com/ en-us/dotnet/fundamentals/networking/http/httpclient#http-proxy

全局默认代理

HttpClient.DefaultProxy 是一个静态属性,用于确定所有代理的默认代理如果在通过其构造函数传递的 HttpClientHandler 中未显式设置代理,则使用 HttpClient 实例。此属性返回的默认实例根据您的平台遵循一组不同的规则进行初始化:

  • 对于 Windows:从环境变量中读取代理配置,或者如果未定义这些变量,则从用户的代理设置中读取代理配置。
  • 对于 Linux:从环境变量中读取代理配置,或者,如果未定义这些配置,则此属性会初始化绕过所有地址的未配置实例。

Windows 和基于 Unix 的平台上用于 ​​DefaultProxy 初始化的环境变量是:

HTTP_PROXY: the proxy server used on HTTP requests.
HTTPS_PROXY: the proxy server used on HTTPS requests.
ALL_PROXY: the proxy server used on HTTP and/or HTTPS requests in case HTTP_PROXY and/or HTTPS_PROXY aren't defined.

您的代理需要身份验证吗?如果它们是公开的,则可能不会,但这可能会对内部网络上的一些人有所帮助。

HttyClientHandler.DefaultProxyCredentials = CredentialsCache.DefaultNetworkCredentials

Your XML configuration worked for .NET Framework but won't work for .NET core. You can either pass in your own Proxy object to HttpClientHandler, as you've already tried, or you need to use environment variables.

https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient#http-proxy

Global default proxy

The HttpClient.DefaultProxy is a static property that determines the default proxy that all HttpClient instances use if no proxy is set explicitly in the HttpClientHandler passed through its constructor. The default instance returned by this property initializes following a different set of rules depending on your platform:

  • For Windows: Reads proxy configuration from environment variables or, if those aren't defined, from the user's proxy settings.
  • For Linux: Reads proxy configuration from environment variables or, in case those aren't defined, this property initializes a non-configured instance that bypasses all addresses.

The environment variables used for DefaultProxy initialization on Windows and Unix-based platforms are:

HTTP_PROXY: the proxy server used on HTTP requests.
HTTPS_PROXY: the proxy server used on HTTPS requests.
ALL_PROXY: the proxy server used on HTTP and/or HTTPS requests in case HTTP_PROXY and/or HTTPS_PROXY aren't defined.

Do your proxies require authentication? Probably not if they're public, but this may help some on internal networks.

HttyClientHandler.DefaultProxyCredentials = CredentialsCache.DefaultNetworkCredentials
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文