How do I find out the browser's proxy settings?

前端 未结 4 893
既然无缘
既然无缘 2020-12-01 02:48

I am writing a command-line tool for Windows that uses libcurl to download files from the internet.

Obviously, the downloading doesn\'t work when the user is behind

相关标签:
4条回答
  • 2020-12-01 03:24

    For Firefox/Seamonkey, the problem is a bit more tricky because of the existence of many profiles.

    If you want to assume there is only one profile then you just need to find prefs.js. You parse the network.proxy.type, and then use it to decide, which related values to read.

    I'm working on some documents for mozilla, so put your followup questions in here (checked wiki box), and I'll try to give you the info you need.

    0 讨论(0)
  • 2020-12-01 03:32

    Here is a complete code sample how to call WinHttpGetIEProxyConfigForCurrentUser method from winhttp.dll library in C#

    [TestClass]
    public class UnitTest1
    {
        [StructLayout(LayoutKind.Sequential)]
        public struct WinhttpCurrentUserIeProxyConfig
        {
            [MarshalAs(UnmanagedType.Bool)]
            public bool AutoDetect;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string AutoConfigUrl;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string Proxy;
            [MarshalAs(UnmanagedType.LPWStr)]
            public string ProxyBypass;
    
        }
    
        [DllImport("winhttp.dll", SetLastError = true)]
        static extern bool WinHttpGetIEProxyConfigForCurrentUser(ref WinhttpCurrentUserIeProxyConfig pProxyConfig);
    
        [TestMethod]
        public void TestMethod1()
        {
            var config = new WinhttpCurrentUserIeProxyConfig();
    
            WinHttpGetIEProxyConfigForCurrentUser(ref config);
    
            Console.WriteLine(config.Proxy);
            Console.WriteLine(config.AutoConfigUrl);
            Console.WriteLine(config.AutoDetect);
            Console.WriteLine(config.ProxyBypass);
        }
    }
    
    0 讨论(0)
  • 2020-12-01 03:44

    There are registry keys for these values that you could get to directly of course. You could also do this in .NET without much hassle at all. I believe the WebClient object negotiates the proxy settings for you based on the current settings. This would look like this in C#:

    using System.Net;
    
    string url = "http://www.example.com";
    WebClient client = new WebClient();
    byte[] fileBuffer = client.DownloadFile(url);
    

    Or something close to that.

    0 讨论(0)
  • 2020-12-01 03:45

    The function you're looking for is WinHttpGetIEProxyConfigForCurrentUser(), which is documented at http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx. This function is used by Firefox and Opera to get their proxy settings by default, although you can override them per-browser. Don't do that, though. The right thing to do (which is what everybody else does) is to just get the IE settings and assume that they're correct, since they almost always are.

    Here's a sample of the relevant logic, which you should adapt for your needs:

    if( WinHttpGetIEProxyConfigForCurrentUser( &ieProxyConfig ) )
    {
        if( ieProxyConfig.fAutoDetect )
        {
            fAutoProxy = TRUE;
        }
    
        if( ieProxyConfig.lpszAutoConfigUrl != NULL )
        {
            fAutoProxy = TRUE;
            autoProxyOptions.lpszAutoConfigUrl = ieProxyConfig.lpszAutoConfigUrl;
        }
    }
    else
    {
        // use autoproxy
        fAutoProxy = TRUE;
    }
    
    if( fAutoProxy )
    {
        if ( autoProxyOptions.lpszAutoConfigUrl != NULL )
        {
            autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
        }
        else
        {
            autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
            autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
        }
    
        // basic flags you almost always want
        autoProxyOptions.fAutoLogonIfChallenged = TRUE;
    
        // here we reset fAutoProxy in case an auto-proxy isn't actually
        // configured for this url
        fAutoProxy = WinHttpGetProxyForUrl( hiOpen, pwszUrl, &autoProxyOptions, &autoProxyInfo );
    }
    
    if ( fAutoProxy )
    {
        // set proxy options for libcurl based on autoProxyInfo
    }
    else
    {
        if( ieProxyConfig.lpszProxy != NULL )
        {
            // IE has an explicit proxy. set proxy options for libcurl here
            // based on ieProxyConfig
            //
            // note that sometimes IE gives just a single or double colon
            // for proxy or bypass list, which means "no proxy"
        }
        else
        {
            // there is no auto proxy and no manually configured proxy
        }
    }
    
    0 讨论(0)
提交回复
热议问题