5

在网4.5,我们与代理的工作是这样的:asp.net核心defaultProxy

<system.net> 
    <!-- --> 
    <defaultProxy enabled="true" useDefaultCredentials="false"> 
     <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False" /> 
     <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" /> 
    </defaultProxy> 

    <settings> 
     <httpWebRequest useUnsafeHeaderParsing="true" /> 
     <servicePointManager expect100Continue="false" /> 
    </settings> 
</system.net> 

但在asp.net核心或测试我们没有找到一个解决类似上述 可能有人请帮助我?

我真的很感谢你的帮助

感谢,问候

回答

4
+0

嗨。 我需要使用defaultProxy,因为我有一个客户端wcf,我通过代理服务器验证了我的提供商的白名单上的IP地址。 –

4

使用HTTP代理服务器在.NET的核心,你必须实现IWebProxy interface.This从System.Net.Primitives.dll assembly.You可以DD它project.json如果不是已经有

例如

"frameworks": { 
    "dotnet5.4": { 
     "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) 

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

+0

非常感谢。 我如何为wcf客户端做到这一点? –

+0

嗨。 我需要使用defaultProxy,因为我有一个客户端wcf,我通过代理服务器验证了我的提供商的白名单上的IP地址。 –

+0

@MarceloOliveto是否在.net内核中使用面向客户端的WCF库?看起来它仍然不支持代理,有一个问题打开它https://github.com/dotnet/wcf/issues/1592 –