Proxy internet属性-读取配置脚本路径
我想读取“使用自动配置脚本”地址字段中的值 我需要像这样设置CefSharp的代理Proxy internet属性-读取配置脚本路径,proxy,cefsharp,pac,Proxy,Cefsharp,Pac,我想读取“使用自动配置脚本”地址字段中的值 我需要像这样设置CefSharp的代理 settings.CefCommandLineArgs.Add("proxy-pac-url","proxy address"); 我尝试了WebRequest.GetSystemWebProxy的不同变体,并在wininet.dll中调用函数InternetQueryOption 来自Github上的CefSharp回购协议的代码 public static class ProxyConfig {
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进行测试
我可以从注册表中读取值,但感觉像是黑客攻击
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;
}
}
}
}
是否尝试了自动检测命令行参数?是的,我首先将代理自动检测设置为默认设置。CefCommandLineArgs.Add(“代理自动检测”,“1”);我不知道任何其他设置,你可以通过挖掘铬源来了解它的功能,并向CEF项目提交PR。