views:

5220

answers:

3

Hello,

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 am just checking if the address of the proxy exists. If there is not, then there is no proxy to set in the webclient.

Is this the correct way to do this:

Many thanks for any advice,

WebProxy proxy = WebProxy.GetDefaultProxy();

        if (proxy.Address.ToString() != string.Empty)
        {
            Console.WriteLine("Proxy URL: " + proxy.Address.ToString());
            wc.Proxy = proxy;
        }

====== Code edit ======

 [DllImport("wininet.dll", CharSet = CharSet.Auto)]
    private extern static bool InternetGetConnectedState(ref InternetConnectionState_e lpdwFlags, int dwReserved);


[Flags]
    enum InternetConnectionState_e : int
    {
        INTERNET_CONNECTION_MODEM = 0x1,
        INTERNET_CONNECTION_LAN = 0x2,
        INTERNET_CONNECTION_PROXY = 0x4,
        INTERNET_RAS_INSTALLED = 0x10,
        INTERNET_CONNECTION_OFFLINE = 0x20,
        INTERNET_CONNECTION_CONFIGURED = 0x40
    }      

    // Return true or false if connecting through a proxy server
    public bool connectingThroughProxy()
    {
        InternetConnectionState_e flags = 0;
        InternetGetConnectedState(ref flags, 0);
        bool hasProxy = false;

        if ((flags & InternetConnectionState_e.INTERNET_CONNECTION_PROXY) != 0)
        {
            hasProxy = true;
        }
        else
        {
            hasProxy = false;
        }

        return hasProxy;
    }
+1  A: 

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
}
John Weldon
+1  A: 

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:

Matthew Flaschen
+3  A: 

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 I believe this is now found under

netsh winhttp {something}

(my current machine is XP, so I can't do a "?" to find the command - it'll be something to do with "import", presumably)

Marc Gravell
I have edited my code by using a p/invoke. This works well telling me if the computing is connecting through a proxy. However, referencing my initial code post. Is this the correct way to get the information about the proxy. i.e. address, port? Thanks.
robUK
Well, I still maintain that configuring WinHTTP is a better option than querying it yourself. At work, all the (non-local) traffic goes via a proxy, and I've never had a problem just using `WebClient` if I've configured WinHTTP first.
Marc Gravell
The problem with this solution is it requires the user to run a command changing system wide settings just to use your application. Under a restricted environment they also may not have access to the command and/or your application may not have access to it.
tjmoore