如何在 HttpClientHandler 上设置代理?
我已经尝试在我的 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>
我读了很多关于 WebProxy、WebRequest 和 HttpClient(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 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 和基于 Unix 的平台上用于 DefaultProxy 初始化的环境变量是:
您的代理需要身份验证吗?如果它们是公开的,则可能不会,但这可能会对内部网络上的一些人有所帮助。
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:
The environment variables used for DefaultProxy initialization on Windows and Unix-based platforms are:
Do your proxies require authentication? Probably not if they're public, but this may help some on internal networks.