我需要使用类项目中的双向 SSL 对外部第 3 方进行 WCF 服务调用。我已将第 3 方提供的 WSDL 作为服务引用添加到我的项目中。问题是我们域 (*.abc.com) 之外的所有调用都通过代理服务器传递
http://ironport:8080
这就是我在代码中所做的 -
var binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
binding.BypassProxyOnLocal = false;
binding.UseDefaultWebProxy = true;
binding.AllowCookies = false;
binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
var endpoint = new EndpointAddress("https://blablabla.com/GetData.svc");
var client = new AccountClient(binding, endpoint);
X509Certificate2 certi = new X509Certificate2(@"path to pfx file", "password");
client.ClientCredentials.ClientCertificate.Certificate = certi;
我使用 - 进行服务调用
var account = client.ExportAccounts(obj1, obj2, obj3);
然后它给了我一个错误 -
The remote server returned an Error (407): Proxy authentication required
这很明显,因为我在任何地方都没有提到请求需要经过的代理详细信息。我需要的是一种将来自不同项目的 web.config 文件的以下信息添加到我上面的请求中的方法 -
<system.net>
<defaultProxy useDefaultCredentials="true">
<proxy proxyaddress="http://ironport:8080" />
<bypasslist>
<add address="[\w]+\.abc\.com$" />
</bypasslist>
</defaultProxy>
</system.net>
有什么方法可以在代码中实现这一点吗?或者我是否需要以完全不同的方式解决这个问题?让我知道是否需要发布更多信息。
最佳答案
您可以尝试使用 WebProxy Class .未经测试,但像这样:
WebProxy proxy = new WebProxy("http://ironport:8080");
proxy.BypassList = new string[] { "[\w]+\.abc\.com$" };
另一种选择是将配置的相关部分移动到正在使用您的类库的应用程序的 web/app.config。
已添加
不能 100% 确定这会起作用,但您可以尝试将此行添加到您的代码中:
WebRequest.DefaultProxy = proxy;
取自此answer
另一种选择可能是使用 WsHttpBinding
的 ProxyAddress
属性(确保在这种情况下将 UseDefaultProxy
设置为 false),但是我看不到用这个添加旁路列表的方法。
关于c# - 使用代理从类项目调用 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21478804/