如何为特定的wcf客户端设置代理凭据?

问题描述 投票:18回答:3

我需要连接到某些公共wcf服务,但是我和服务之间存在一些代理。如果我使用默认的代理设置,例如

<system.net>
  <defaultProxy useDefaultCredentials="true" />
</system.net>

HttpWebRequest.DefaultWebProxy

效果很好但是我不需要为整个应用程序设置代理设置,我需要为特定的连接设置它。那我该怎么做?

我看到ProxyAddress属性

(client.Endpoint.Binding as BasicHttpBinding).ProxyAddress

但是没有凭据的任何属性...我正在考虑以某种方式修改HttpWebRequest,但我不知道如何获取它...

已解决

谢谢大家的回答。

适合解决我的问题的AntonK答案。

当这个问题成为现实时,我以相同的方式解决了问题,但是没有使用web.config并编写了此方法

void SetProxySettings<TChannel>(ClientBase<TChannel> client, 
    bool useProxy, string address, int port, string login, string password) 
    where TChannel : class
{
    if (!useProxy) return;
    var b = client.Endpoint.Binding as BasicHttpBinding;
    if (b == null)
    {
        System.Diagnostics.Debug.WriteLine("Binding of this endpoint is not BasicHttpBinding");
        return;
    }
    b.ProxyAddress = new Uri(string.Format("http://{0}:{1}", address, port));
    b.UseDefaultWebProxy = false; // !!!
    b.Security.Mode = BasicHttpSecurityMode.Transport;
    b.Security.Transport.ClientCredentialType = HttpClientCredentialType.None; // !!!
    b.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic; // !!!
    if (client.ClientCredentials == null) return;
    client.ClientCredentials.UserName.UserName = login;
    client.ClientCredentials.UserName.Password = password;
}
c# wcf proxy
3个回答
7
投票
总之,这是在web.config中设置特定服务的代理的方法。在绑定配置中,设置

proxyAddress =“ http:// myproxy:8080”

并设置

useDefaultWebProxy =“ false”


0
投票
希望有帮助

0
投票
System.ServiceModel.Security System.ServiceModel.NetTcp System.ServiceModel.Http
© www.soinside.com 2019 - 2024. All rights reserved.