System.Net.WebProxy忽略本地请求-有没有办法处理它?

时间:2018-09-04 06:36:24

标签: proxy localhost httpclient hosts webproxy

我想通过也在本地主机上设置的代理向本地rest api发送请求。

为此,我准备了以下设置:

    public Task<string> MakeCall(ICredentials credentials, string targetUrl)
    {
        var clientHandler = new HttpClientHandler()
        {
            Proxy = ConfigureProxy(),
            UseProxy = true,
        };

        var client = new HttpClient(clientHandler, true);
        var resp = client.GetAsync(targetUrl).Result.Content.ReadAsStringAsync();
        return resp;
    }

    private static WebProxy ConfigureProxy()
    {
        return new WebProxy
        {
            Address = new Uri("http://localhost:8082"),
            BypassProxyOnLocal = false
        };
    }

它起作用,但是当targetUrl是本地主机地址时不起作用。我在网上找到了这个解释得很好的答案:

How to make System.Net.WebProxy to not bypass local urls?

还有这篇文章:

Microsoft implementation is hardcoded

所以我有一个问题-有什么办法可以绕过这种行为?我想通过代理(在localhost上)向rest api(在localhost上)发送请求。我在某个地方找到了“ -loopback”标志,但这似乎对我不起作用。

我玩过hosts文件,试图为我的本地地址设置别名,这样webproxy不会拒绝它。在那种情况下,Ping会以某种方式响应我创建的Ping,但httpclient无法识别它。

预先感谢

0 个答案:

没有答案