首页 > 解决方案 > 互联网属性 - 读取配置脚本路径

问题描述

我想阅读“使用自动配置脚本”地址字段中的值。

我需要像这样为 CefSharp 设置代理

settings.CefCommandLineArgs.Add("proxy-pac-url","proxy address");

我已经尝试过 WebRequest.GetSystemWebProxy 的不同变体,并在 wininet.dll 中调用函数 InternetQueryOption。

来自 Github 上 CefSharp 存储库的代码

 public static class ProxyConfig
{
    [DllImport("wininet.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool InternetQueryOption(IntPtr hInternet, uint dwOption, IntPtr lpBuffer, ref int lpdwBufferLength);

    private const uint InternetOptionProxy = 38;

    public static InternetProxyInfo GetProxyInformation()
    {
        var bufferLength = 0;
        InternetQueryOption(IntPtr.Zero, InternetOptionProxy, IntPtr.Zero, ref bufferLength);
        var buffer = IntPtr.Zero;

        try
        {
            buffer = Marshal.AllocHGlobal(bufferLength);

            if (InternetQueryOption(IntPtr.Zero, InternetOptionProxy, buffer, ref bufferLength))
            {
                var ipi = (InternetProxyInfo)Marshal.PtrToStructure(buffer, typeof(InternetProxyInfo));
                return ipi;
            }
            {
                throw new Win32Exception();
            }
        }
        finally
        {
            if (buffer != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(buffer);
            }
        }
    }
}

如果 Windows 设置中有代理,则此代码有效,易于使用 Fiddler 进行测试。

我可以从注册表中读取值,但感觉就像是 hack

  RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", false);
  var autoConfigUrl = registry?.GetValue("AutoConfigURL");

必须有一个“正确”的方法来做到这一点?

当前测试代码:

if (settingsViewModel.UseProxy)
        {
            // https://securelink.be/blog/windows-proxy-settings-explained/
            RegistryKey registry = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", false);
            var autoConfigUrl = registry?.GetValue("AutoConfigURL").ToString();
            var proxyEnable = registry?.GetValue("ProxyEnable").ToString();
            if (!string.IsNullOrEmpty(autoConfigUrl) && !string.IsNullOrEmpty(proxyEnable) && proxyEnable == "1")
            {
                settings.CefCommandLineArgs.Add("proxy-pac-url", autoConfigUrl);
            }
            else
            {
                var proxy = ProxyConfig.GetProxyInformation();
                switch (proxy.AccessType)
                {
                    case InternetOpenType.Direct:
                        {
                            //Don't use a proxy server, always make direct connections.
                            settings.CefCommandLineArgs.Add("no-proxy-server", "1");
                            break;
                        }
                    case InternetOpenType.Proxy:
                        {
                            settings.CefCommandLineArgs.Add("proxy-server", proxy.ProxyAddress.Replace(' ', ';'));
                            break;
                        }
                    case InternetOpenType.PreConfig:
                        {
                            settings.CefCommandLineArgs.Add("proxy-auto-detect", "1");
                            break;
                        }
                }
            }
        }

在此处输入图像描述

标签: proxycefsharppac

解决方案


推荐阅读