因为.net core 的System.Net 中没有提供WebProxy这个方法,所以可以根据需求实现一个.
public class CoreWebProxy : IWebProxy
{
public readonly Uri Uri;
private readonly bool bypass;
public CoreWebProxy(Uri uri, ICredentials credentials = null, bool bypass = false)
{
Uri = uri;
this.bypass = bypass;
Credentials = credentials;
}
public ICredentials Credentials { get; set; }
public Uri GetProxy(Uri destination) => Uri;
public bool IsBypassed(Uri host) => bypass;
public override int GetHashCode()
{
if (Uri == null)
{
return -1;
}
return Uri.GetHashCode();
}
}
参考:
– Specify proxy in asp.net core rc2
– .Net Core中使用Proxy请求数据