I'm writing a program that should work without proxy and with proxy with authentication – automatically! It should call a WCF service. In this example the instance is called client
. I also use a self written class (proxyHelper
) that requests the credentials.
BasicHttpBinding connection = client.Endpoint.Binding as BasicHttpBinding;<br/>
connection.ProxyAddress = _???_<br/>
connection.UseDefaultWebProxy = false;<br/>
connection.BypassProxyOnLocal = false;<br/>
connection.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.Basic;<br/>
client.ClientCredentials.UserName.UserName = proxyHelper.Username;
client.ClientCredentials.UserName.Password = proxyHelper.Password;
I'm facing a problem getting the ProxyAddress. If I use HttpWebRequest.GetSystemWebProxy()
to get actual defined proxy I see in debug mode the correct proxy address but it's a non public property. Setting UseDefaultWebProxy to true doesn't work and if I add the proxy address hard-coded and set the UseDefaultWebProxy to false it works fine. So… how can I gather the address of the default web proxy?
Best Answer
The proxy has a method called GetProxy which can be used to get the Uri of the proxy.
Here's a snippet of the description from MSDN:
You can use the following code to get the proxy details. Note that the Uri that you pass to the GetProxy method is important, as it will only return you the proxy credentials if the proxy isn't bypassed for the specified Uri.