Asp.net web api 在asp.net core rc2中指定代理
我正在尝试在dotnet核心应用程序中使用WebAPI为请求指定web代理。当我瞄准实际的clr(dnx46)时,这段代码曾经有效,但现在我尝试使用rc2的东西,说支持的框架是netcoreapp1.0和netstandard1.5Asp.net web api 在asp.net core rc2中指定代理,asp.net-web-api,asp.net-core,dotnet-httpclient,coreclr,Asp.net Web Api,Asp.net Core,Dotnet Httpclient,Coreclr,我正在尝试在dotnet核心应用程序中使用WebAPI为请求指定web代理。当我瞄准实际的clr(dnx46)时,这段代码曾经有效,但现在我尝试使用rc2的东西,说支持的框架是netcoreapp1.0和netstandard1.5 var clientHandler = new HttpClientHandler{ Proxy = string.IsNullOrWhiteSpace(this._clientSettings.ProxyUrl) ? null : new WebProxy
var clientHandler = new HttpClientHandler{
Proxy = string.IsNullOrWhiteSpace(this._clientSettings.ProxyUrl) ? null : new WebProxy (this._clientSettings.ProxyUrl, this._clientSettings.BypassProxyOnLocal),
UseProxy = !string.IsNullOrWhiteSpace(this._clientSettings.ProxyUrl)
};
我想知道WebProxy类去了哪里。我在任何地方都找不到它,甚至在github存储库中也找不到。如果它从WebProxy更改为什么?
我需要能够将代理设置为特定请求的特定url,因此使用“全局Internet Explorer”的方式无法满足我的需要。这主要是为了调试web请求/响应目的。今天遇到了同样的问题。事实证明,我们必须提供自己的
IWebProxy
实现。幸运的是,这一点都不复杂:
public class MyProxy : IWebProxy
{
public MyProxy(string proxyUri)
: this(new Uri(proxyUri))
{
}
public MyProxy(Uri proxyUri)
{
this.ProxyUri = proxyUri;
}
public Uri ProxyUri { get; set; }
public ICredentials Credentials { get; set; }
public Uri GetProxy(Uri destination)
{
return this.ProxyUri;
}
public bool IsBypassed(Uri host)
{
return false; /* Proxy all requests */
}
}
你可以这样使用它:
var config = new HttpClientHandler
{
UseProxy = true,
Proxy = new MyProxy("http://127.0.0.1:8118")
};
using (var http = new HttpClient(config))
{
var ip = http.GetStringAsync("https://api.ipify.org/").Result;
Console.WriteLine("Your IP: {0}");
}
在您的特定情况下,您甚至可以在IWebProxy实现中加入确定是否需要代理的逻辑。我遇到过类似的问题,但有一个不同之处。我需要使用默认的系统代理。但是,对于.net framework,将代理设置为null确实有效。如果我错了,请更正。如何使.net core使用默认系统代理?