在 .NET 4.5 HttpClient 中使用代理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16526689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 12:14:59  来源:igfitidea点击:

Using a proxy with .NET 4.5 HttpClient

.netasp.net-web-api.net-4.5

提问by Mike Ruhlin

I'm troubleshooting a bug with a service I call through .NET's HttpClient, trying to route the request through a local proxy (Fiddler), but my proxy settings seem to not be taking effect.

我正在解决我通过 .NET 的 HttpClient 调用的服务的错误,尝试通过本地代理 (Fiddler) 路由请求,但我的代理设置似乎没有生效。

Here's how I create the client:

这是我创建客户端的方法:

private HttpClient CreateHttpClient(CommandContext ctx, string sid) {
    var cookies = new CookieContainer();

    var handler = new HttpClientHandler {
        CookieContainer = cookies,
        UseCookies = true,
        UseDefaultCredentials = false,
        Proxy = new WebProxy("http://localhost:8888", false, new string[]{}),
        UseProxy = true,
    };

    // snip out some irrelevant setting of authentication cookies

    var client = new HttpClient(handler) {
        BaseAddress = _prefServerBaseUrl,
    };

    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/json"));

    return client;
}

then I send the request by:

然后我通过以下方式发送请求:

var response = CreateHttpClient(ctx, sid).PostAsJsonAsync("api/prefs/", smp).Result;

Request goes straight to the server without attempting to hit the proxy. What did I miss?

请求直接发送到服务器而不尝试访问代理。我错过了什么?

采纳答案by Mike Ruhlin

Ah, The BaseAddress I was pointing to was http://localhost:8081. Turns out that despite setting BypassOnLocal to false, evidently localhost is still special enough that it bypasses the proxy.

啊,我指向的 BaseAddress 是http://localhost:8081. 事实证明,尽管将 BypassOnLocal 设置为 false,但显然 localhost 仍然足够特殊,可以绕过代理。

I added a domain binding in IIS, host file entry to point that domain to 127.0.0.1, used newly created domain, now it works.

我在 IIS 中添加了一个域绑定,主机文件条目将该域指向 127.0.0.1,使用新创建的域,现在它可以工作了。

回答by NathanAldenSr

This code worked for me:

这段代码对我有用:

var httpClientHandler = new HttpClientHandler
                        {
                            Proxy = new WebProxy("http://localhost:8888", false),
                            UseProxy = true
                        }

Note that I am not supplying an empty array to my WebProxyconstructor. Perhaps that's the problem?

请注意,我没有向WebProxy构造函数提供空数组。也许这就是问题所在?

回答by Badri

Is Fiddler configured to capture traffic from all processes? Look at the status bar where you see "Capturing". It should show "All Processes" next to it. If it shows "Web browsers", click it and change it to all processes. This could be different depending on the version of Fiddler you use.

Fiddler 是否配置为从所有进程捕获流量?查看您看到“正在捕获”的状态栏。它应该在它旁边显示“所有进程”。如果它显示“Web 浏览器”,请单击它并将其更改为所有进程。这可能会有所不同,具体取决于您使用的 Fiddler 版本。