asp.net核心defaultProxy

在net 4.5中,我们正在使用这样的代理:

           

但是在asp.net核心或测试中我们找不到像上面这样的解决方案有人可以帮帮我吗?

我非常感谢你的帮助

感谢和问候

要在.net核心中使用HTTP代理,您必须实现IWebProxy接口。这来自System.Net.Primitives.dll程序集。 您可以将它添加到project.json如果尚未添加)

例如

 "frameworks": { "dotnet4.5": { "dependencies": { "System.Net.Primitives": "4.3.0" } } } 

实施非常简单

 public class MyHttpProxy : IWebProxy { public MyHttpProxy() { //here you can load it from your custom config settings this.ProxyUri = new Uri(proxyUri); } public Uri ProxyUri { get; set; } public ICredentials Credentials { get; set; } public Uri GetProxy(Uri destination) { return this.ProxyUri; } public bool IsBypassed(Uri host) { //you can proxy all requests or implement bypass urls based on config settings return false; } } var config = new HttpClientHandler { UseProxy = true, Proxy = new MyHttpProxy() }; //then you can simply pass the config to HttpClient var http = new HttpClient(config) 

checkout https://msdn.microsoft.com/en-us/library/system.net.iwebproxy(v=vs.100).aspx

你应该使用中间件。 你有没看过这个:

https://github.com/aspnet/Proxy

有一个’samples’文件夹: https : //github.com/aspnet/Proxy/tree/dev/samples/Microsoft.AspNetCore.Proxy.Samples

关于该主题的其他资源: http : //josephwoodward.co.uk/2016/07/proxying-http-requests-asp-net-core-using-kestrel

http://overengineer.net/creating-a-simple-proxy-server-middleware-in-asp-net-core

中间件是否适合您?

另一个未完成的工作是:

将.NET Core应用程序部署到Web服务器后(我的是IIS)。 根文件夹中实际上有一个web.config文件。

手动添加

      

或者您的具体设置将起到魔力

仅适用于服务器。 您的本地构建仍然无法正常工作。

虽然可以在可以使用HttpClientHander的情况下手动设置代理,但是在没有代码的情况下默认所有请求,就像在.NET Framework中一样,目前无法实现。 如果您使用的是不公开此function的库,那将是多么糟糕。

GitHub中的以下问题是使用环境变量将此function引入.NET Core: https : //github.com/dotnet/corefx/issues/24574

我会建议任何正在寻找解决方案的人来检查当前的实现状态。