我正在尝试使用 HttpClient
与代理后面的api进行通信。但是因为代理只对当前环境有效,我不想把它硬编码。
目前我的做法如下:
public static HttpClient CreateClient()
{
var cookies = new CookieContainer();
var handler = new HttpClientHandler
{
CookieContainer = cookies,
UseCookies = true,
UseDefaultCredentials = false,
UseProxy = true,
Proxy = new WebProxy("proxy.dev",1234),
};
return new HttpClient(handler);
}
这是我想使用的内容:
<system.net>
<defaultProxy>
<proxy bypassonlocal="true"
usesystemdefault="false"
proxyaddress="http://proxy.dev:1234" />
</defaultProxy>
</system.net>
有没有可能在应用程序/ web.config 中定义代理,并将其默认使用在我的HttpClient中?
谢谢您提供的任何想法。