C# 2008 SP1
I am using the code to detect if a proxy has been set under \"Internet Options\". If there is a proxy then I will set this in my webclient.
So I
Check out the System.Net.Configuration.ProxyElement class. That may have info you're looking for.
What you describe works, you can also look in the registry.
Here's a powershell script I wrote to check out the proxy:
function get-proxy
{
$path = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$reg = get-itemproperty $path
return $reg
}
<system.net>
<defaultProxy enabled="false" useDefaultCredentials="false">
<proxy/>
<bypasslist/>
<module/>
</defaultProxy>
Use this snippet in application.config file.
First, GetDefaultProxy is marked as deprecated so you have no guarantee it will be around in even the immediate future. Second, Address can be null so the code you gave risks a NullReferenceException:
It appears that WebRequest.DefaultWebProxy is the official replacement for WebProxy.GetDefaultProxy.
You should be able to drop that in to your original code with only a little modification. Something like:
WebProxy proxy = (WebProxy) WebRequest.DefaultWebProxy;
if (proxy.Address.AbsoluteUri != string.Empty)
{
Console.WriteLine("Proxy URL: " + proxy.Address.AbsoluteUri);
wc.Proxy = proxy;
}
Setting my request proxy to WebRequest.GetSystemWebProxy()
solved the problem.
WebProxy.GetDefaultProxy()
is the actual way but it is now deprecated.
WebClient
etc use the WinHTTP settings (not the IE settings), so the easiest thing to do is to configure WinHTTP! On XP etc you can use:
proxycfg -u
to import the current IE settings into the WinHTTP store. After that, WebClient
etc should be able to use the same settings without issue. On Vista and Windows 7 this is now found under:
netsh winhttp import proxy ie
You need to run this as administrator.