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

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

   

要么

 HttpWebRequest.DefaultWebProxy 

它工作得很好,但我不需要为整个应用程序设置代理设置,我需要为特定连接设置它。 那我该怎么做呢?

我看到了ProxyAddress属性

 (client.Endpoint.Binding as BasicHttpBinding).ProxyAddress 

但是没有任何凭据属性…我想以某种方式修改HttpWebRequest,但我不知道如何得到它…

解决了

谢谢大家的答案。

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

在这个问题是实际的时候,我以同样的方式解决了它,但没有使用web.config并编写了这个方法

 void SetProxySettings(ClientBase 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; } 

这是一篇关于这个问题的文章。

http://blogs.msdn.com/b/stcheng/archive/2008/12/03/wcf-how-to-supply-dedicated-credentials-for-webproxy-authentication.aspx

总之,这是如何在web.config中为特定服务设置代理。 在绑定配置中,设置proxyAddress =“http:// myproxy:8080”并设置useDefaultWebProxy =“false”

            

你可以试试这个

 HttpWebRequest request = HttpWebRequest.Create("URI") as HttpWebRequest; var proxy = new WebProxy(HttpWebRequest.GetSystemWebProxy().GetProxy(request.RequestUri), true); proxy.Credentials = new NetworkCredential(proxyUserName, proxyPassword, DomainName); request.Proxy = proxy; 

希望能帮助到你