proxy - 互联网属性 - 读取配置脚本路径
问题描述
我想阅读“使用自动配置脚本”地址字段中的值。
我需要像这样为 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;
}
}
}
}
解决方案
推荐阅读
- python - 绕过 \r 函数的方法
- javascript - 当我将鼠标悬停在字母上时,如何使字母停止移动?
- css - Flexbox 将几个项目向左对齐,几个向中心对齐,几个向右对齐 - 顺序应该是可变的
- qt - QModbusTcpClient 无法连接到 PLC
- javascript - 点击时复制输入
- php - 如何在这个数组中打印出“id”?
- c - 如何正确地将元素打印到文件中?
- c# - 如何将放置在拆分按钮内容内的标签文本与选定的列表框项目绑定?
- java - 无法初始化类 io.jsonwebtoken.SignatureAlgorithm,java.lang.NoClassDefFoundError,
- excel - 如何使用 Excel 中的另一个按钮调用另一个工作表中的按钮?