windows 如何找出浏览器的代理设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/202547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:33:51  来源:igfitidea点击:

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

windowsinternet-explorerfirefoxproxybrowser

提问by rix0rrr

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

我正在为 Windows 编写一个命令行工具,它使用 libcurl 从互联网下载文件。

Obviously, the downloading doesn't work when the user is behind a proxy server, because the proxy needs to be configured. I want to keep my tool as simple as possible however, and not have to burden the user with having to configure the proxy. My tool doesn't even have a config file, so the user would otherwise have to pass in the proxy settings on every command, or set an environment variable or somesuch -- way too much hassle.

显然,当用户在代理服务器后面时下载不起作用,因为需要配置代理。然而,我想让我的工具尽可能简单,而不必让用户不得不配置代理。我的工具甚至没有配置文件,因此用户必须在每个命令上传递代理设置,或者设置环境变量或类似的东西——太麻烦了。

So I thought, everyone's browser will usually already be set up properly, proxy configured and everything. This will be true for even the most basic user because otherwise "their internet wouldn't work".

所以我想,每个人的浏览器通常都已经正确设置,代理配置等等。即使是最基本的用户也是如此,否则“他们的互联网将无法工作”。

So I figure that I can find out whether to use a proxy by looking at IE's proxy settings.

所以我想我可以通过查看IE的代理设置来确定是否使用代理。

How do I go about this? More specifically:

我该怎么做?进一步来说:

  • Is there one set of "proxy settings" in Windows, used by all browsers (probably IE's), or would I have to write different routines for IE, Firefox, Opera, etc?
  • I know that I can probably read the values directly out of the appropriate registry locations if they are configured manually, but does this also work with "automatically detect proxy server?" Do I even have to bother with that option, or is it (almost) never used?
  • Windows 中是否有一组“代理设置”,供所有浏览器(可能是 IE)使用,或者我是否必须为 IE、Firefox、Opera 等编写不同的例程?
  • 我知道如果手动配置它们,我可能可以直接从适当的注册表位置读取值,但这也适用于“自动检测代理服务器”吗?我什至需要为那个选项而烦恼,还是(几乎)从未使用过它?

Before people start suggesting alternatives: I'm using C, so I'm limited to the Win32 API, and I really really want to keep using C and libcurl.

在人们开始建议替代方案之前:我使用的是 C,所以我只能使用 Win32 API,而且我真的很想继续使用 C 和 libcurl。

回答by JSB????

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.

您正在寻找的函数是 WinHttpGetIEProxyConfigForCurrentUser(),它记录在http://msdn.microsoft.com/en-us/library/aa384096(VS.85).aspx。默认情况下,Firefox 和 Opera 使用此函数来获取其代理设置,但您可以针对每个浏览器覆盖它们。不过不要那样做。正确的做法(其他人都这样做)是获取 IE 设置并假设它们是正确的,因为它们几乎总是正确的。

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
    }
}

回答by Maksym Kozlenko

Here is a complete code sample how to call WinHttpGetIEProxyConfigForCurrentUsermethod from winhttp.dlllibrary in C#

这是一个完整的代码示例,如何在 C# 中WinHttpGetIEProxyConfigForCurrentUserwinhttp.dll库中调用方法

[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);
    }
}

回答by justin.m.chase

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#:

当然,您可以直接访问这些值的注册表项。您也可以在 .NET 中轻松完成此操作。我相信 WebClient 对象会根据当前设置为您协商代理设置。这在 C# 中看起来像这样:

using System.Net;

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

Or something close to that.

或者接近那个的东西。

回答by benc

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

对于 Firefox/Seamonkey,由于存在许多配置文件,所以问题有点棘手。

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.

如果您想假设只有一个配置文件,那么您只需要找到 prefs.js。您解析 network.proxy.type,然后使用它来决定要读取哪些相关值。

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.

我正在为 mozilla 编写一些文档,所以将您的后续问题放在此处(选中 wiki 框),我会尽力为您提供所需的信息。