无法在 Windows 7 上查询代理“自动检测设置”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2871510/
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-15 14:29:28  来源:igfitidea点击:

Unable to query proxy "Automatically Detect Settings" on windows 7

windowswindows-7proxywininet

提问by user345794

I am trying to capture proxy setting ("Automatically Detect Settings"). My code works on XP and Vista. But it is NOT working on Windows 7

我正在尝试捕获代理设置(“自动检测设置”)。我的代码适用于 XP 和 Vista。但它不适用于 Windows 7

Please see the details of target platform

请查看目标平台详情

Windows 7 Enterprise, IE 8.0.7600.16385, Wininet.dll 8.0.7600.16535

Windows 7 企业版、IE 8.0.7600.16385、Wininet.dll 8.0.7600.16535

Please see the code snippet

请看代码片段

INTERNET_PER_CONN_OPTION_LIST List;
INTERNET_PER_CONN_OPTION Option[1];

unsigned long nSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
Option[0].dwOption = INTERNET_PER_CONN_FLAGS;
List.dwSize = sizeof(INTERNET_PER_CONN_OPTION_LIST);
List.pszConnection = NULL;
List.dwOptionCount = 1;
List.dwOptionError = 0;
List.pOptions = Option;

if(!InternetQueryOption(NULL, INTERNET_OPTION_PER_CONNECTION_OPTION, &List, &nSize))
    AfxMessageBox(L"InternetQueryOption failed! (%d)\n");

TCHAR a[100];
swprintf(a, L"Flag value is : %d",Option[0].Value.dwValue, 80); 
AfxMessageBox(a);

But on Windows 7, even if "Automatically Detect Settings" option in IE is checked, the program says that that flag is not set

但是在 Windows 7 上,即使选中了 IE 中的“自动检测设置”选项,程序也会说未设置该标志

I tried with WinHttp api also as shown below.

我也尝试使用 WinHttp api,如下所示。

WINHTTP_CURRENT_USER_IE_PROXY_CONFIG stProxyConfig = {0};

WinHttpGetIEProxyConfigForCurrentUser(&stProxyConfig);

if (stProxyConfig.fAutoDetect == TRUE)
    AfxMessageBox(L"Auto proxy detection enabled");
else
    AfxMessageBox(L"Auto proxy detection disabled");

On Windows 7, above code also fails to capture the "Automatically Detect Settings" option. Any input on this is highly appreciated.

在 Windows 7 上,上述代码也无法捕获“自动检测设置”选项。对此的任何投入都受到高度赞赏。

Thanks John

谢谢约翰

回答by EricLaw

This is a new performance-optimizing feature introduced in IE8 called SmartWPAD.

这是 IE8 中引入的一项新的性能优化功能,称为 SmartWPAD。

WinINET keeps track of whether a given network has a WPAD server (e.g. what the Automatically Detect feature is used to look for). If the network does not have a WPAD server, then WinINET effectively "masks out" the "Use autodetect" bit when you do the InternetQueryOption so that your code doesn't waste a ton of time doing a proxy lookup that will return no proxy on this network.

WinINET 会跟踪给定的网络是否具有 WPAD 服务器(例如,自动检测功能用于查找什么)。如果网络没有 WPAD 服务器,那么当您执行 InternetQueryOption 时,WinINET 会有效地“屏蔽”“使用自动检测”位,这样您的代码就不会浪费大量时间进行代理查找而不会返回任何代理这个网络。

If you MUST get the UI state (defeating the WinINET SWPAD feature) because, for instance, you plan to take this information and cache it for use on some other network, or something similar, then you must query for INTERNET_PER_CONN_FLAGS_UI first-- when you use this option, you will get back the UI state, independent of the SWPAD feature.

如果您必须获取 UI 状态(击败 WinINET SWPAD 功能),因为例如,您计划获取此信息并将其缓存以在其他网络或类似网络上使用,那么您必须首先查询 INTERNET_PER_CONN_FLAGS_UI——当您使用此选项,您将获得独立于 SWPAD 功能的 UI 状态。

If this query fails, then the system is running a previous version of Internet Explorer and the client should query again with INTERNET_PER_CONN_FLAGS.

如果此查询失败,则系统正在运行以前版本的 Internet Explorer,客户端应使用 INTERNET_PER_CONN_FLAGS 再次查询。

回答by Shankar Arigela

I have a C# code snippet where you can Check/Uncheck 'Automatically detect settings' check box of IE Connection Settings. You can find what you are looking for in this snippet.

我有一个 C# 代码片段,您可以在其中选中/取消选中 IE 连接设置的“自动检测设置”复选框。您可以在此代码段中找到您要查找的内容。

    public bool IsIEAutoDetectProxy(bool set)
    {
        // Setting Proxy information for IE Settings.
        RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true);
        byte[] defConnection = (byte[])RegKey.GetValue("DefaultConnectionSettings");
        if (defConnection[8] == Convert.ToByte(9))
           return true;
        else
           return false;
    }