可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have a really strange situation here: I've written an app that, among other things, switches connections' proxy from on to off and the other way round. It is done by changing the value in the registry:
public void SetUpProxy(string proxy, bool enable)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
if (proxy != null)
key.SetValue("ProxyServer", proxy);
key.SetValue("ProxyEnable", enable ? 1 : 0);
key.Close();
}
When I request enabling the proxy in my app the first thing it does is to connect to ftp server, download a file, THEN enable the proxy (downloading wouldn't work with the proxy on). Everything works perfectly fine - until I launch Internet Explorer.
For example: if I start my app, let it enable the proxy, then let it disable the proxy - everything works fine. But if I enable the proxy, then launch IE, disable proxy and try enabling it again it doesn't work - the app cannot connect to the ftp server because somehow it uses proxy, even though the value in the registry is 0!
I hope I managed to explain it properly. My question is: why is it happening and how can I fix this?
Edit: I'm using WebClient class to download the file. I've found out that client.Proxy.GetProxy(myUri) returns the specified Uri when tha app runs fine, butwhen I open IE it changes to "http://theUriFromIE".
回答1:
On my own tests, I've come to the conclusion that the Network Connection Settings are , at least on Windows XP, reloaded each time that Internet Explorer is reloaded.
So, to get the changes on the proxy settings loaded, you have to close all instances of IE, and reopen at least one instance, to get the settings loaded.
Don't know if on Windows Vista with the new ways are instant.
Also, look at this other question:
This depends somewhat on your exact needs. If you are writing a C# app and simply want to set the default proxy settings that your app will use, use the class System.Net.GlobalProxySelection. You can also set the proxy for any particular connection with System.Net.WebProxy.
If you actually want to update the proxy settings in the registry, I believe that you'll need to use P/Invoke to call the WinAPI function WinHttpSetDefaultProxyConfiguration.
You also have this Microsoft How to programmatically query and set proxy settings under Internet Explorer.
回答2:
I assume you're using FtpWebRequest, which has a .Proxy property, which in turn has an .IsBypassed property. You may be able to do what you want by setting this property to False (instead of writing to the Registry), and thus be unaffected by anything that happens with IE.
Update: er, I meant set IsBypassed to True (so that the Proxy is bypassed, which I think is what you want).
Update 2: here's another guess. Try putting this in your application's config file and then recompile it:
<configuration>
<system.net>
<defaultProxy>
<proxy autoDetect="false" />
</defaultProxy>
</system.net>
</configuration>
The autoDetect property determines whether your app picks up the IE proxy settings, so setting it to false might prevent your problem.
回答3:
Simply setting WebClient's Proxy property to null helped.
回答4:
Writing to the registry directly may happen to work, but it's not supported, and does not take effect immediately.
To set the proxy correctly, you should be calling the InternetSetOption API in WinINET.
if (InternetSetOptionList((IntPtr)0, INTERNET_OPTION_PER_CONNECTION_OPTION, ref Request, size))
{
// Success. Announce to the world that we've changed the proxy
InternetSetOption((IntPtr)0, INTERNET_OPTION_PROXY_SETTINGS_CHANGED, (IntPtr)0, 0);
}
回答5:
As EricLaw pointed out, the correct way is to go through the wininet library.
Slightly modified is the code I'm using in my C++ application to tell IE to reload it's proxy settings:
// ... code changing registry settings...
InternetSetOption(NULL, INTERNET_OPTION_SETTINGS_CHANGED, NULL, 0);
InternetSetOption(NULL, INTERNET_OPTION_REFRESH, NULL, 0);
Going through wininet.h, I never saw any reference to
INTERNET_OPTION_PROXY_SETTINGS_CHANGED
being defined, so not sure where that comes from.
回答6:
I have used the below udpate 2. Its working. Thanks All.
Update 2: here's another guess. Try putting this in your application's config file and then recompile it: