C# 检测默认网络浏览器的代理设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1023635/
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
Detect proxy settings of default web browser
提问by Sameet
MSDN sample
MSDN 示例
HttpWebRequest myWebRequest=(HttpWebRequest)WebRequest.Create("http://www.microsoft.com");
WebProxy myProxy=new WebProxy();
// Obtain the 'Proxy' of the Default browser.
myProxy=(WebProxy)myWebRequest.Proxy;
Doesn't work. The error I get is: Unable to cast object of type 'WebProxyWrapper' to type 'System.Net.WebProxy'
不起作用。我得到的错误是:无法将“WebProxyWrapper”类型的对象转换为“System.Net.WebProxy”
What options do I have?
我有哪些选择?
回答by adrianbanks
HttpWebRequest.Proxyreturns an IWebProxyinterface, not WebProxy. Change that and it will work.
HttpWebRequest.Proxy返回一个IWebProxy接口,而不是WebProxy. 改变它,它会起作用。
You can also use WebRequest.DefaultWebProxyor WebRequest.GetSystemWebProxy()to get the proxy details instead of making an HttpWebRequestand getting the proxy from that.
您还可以使用WebRequest.DefaultWebProxy或WebRequest.GetSystemWebProxy()来获取代理详细信息,而不是创建HttpWebRequest并从中获取代理。
回答by Quang Minh
To check automatically detect setting, use code:
要检查自动检测设置,请使用代码:
RegistryKey registry = Registry.CurrentUser.OpenSubKey(
"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true);
registry.SetValue("ProxyEnable", 0);
RegistryKey registry2 = Registry.CurrentUser.OpenSubKey(
"SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections", true);
registry2.DeleteValue("DefaultConnectionSettings", false);
registry2.DeleteValue("SavedLegacySettings", false);

