Edw*_*ard 4 asp.net-web-api dotnet-httpclient coreclr asp.net-core
我正在尝试使用dotnet核心应用程序中的WebAPI为请求指定Web代理.当我定位实际的clr(dnx46)时,这段代码曾经工作但是,现在我正在尝试使用rc2的东西,说支持的框架是netcoreapp1.0和netstandard1.5.
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)
};
Run Code Online (Sandbox Code Playgroud)
我想知道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 */
}
}
Run Code Online (Sandbox Code Playgroud)
你会像这样使用它:
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}");
}
Run Code Online (Sandbox Code Playgroud)
在您的特定情况下,您甚至可以通过逻辑确定IWebProxy实现中是否需要代理.
归档时间: |
|
查看次数: |
4743 次 |
最近记录: |