I have set m开发者_StackOverflowy Internet Explorer proxy using the following code.
RegistryKey RegKey = Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
RegKey.SetValue("ProxyServer", "proxyserver");
RegKey.SetValue("ProxyEnable", 1);
RegKey.SetValue("ProxyOverride", "domains;<local>", RegistryValueKind.String);
After running this code I can see that the proxy server name available in Internet Explorer's proxy setting. But when I hit the webpage in my test environment I can't see it. I found very weird behavior of Internet Explorer that after setting the proxy through this code I need to click the OK button in LAN setting and then hit the webpage I can see it properly.
I have searched for it for 4-5 hours and now very much confused with this. Any help will be greatly appreciated.
There is an API for Internet Explorer that should be used for changing the settings.
Wininet reference: http://msdn.microsoft.com/en-us/library/aa385483(VS.85).aspx
After you have changed the proxy settings you must call the InternetSetOption function with the refresh flags to force Internet Explorer to read the registry and repopulate it's settings. If you have already changed the values in the registry you can get away with just calling the following function (RefreshInternetExplorerSettings) afterwards to cause the refresh of IE.
[DllImport("wininet.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern bool InternetSetOption(
IntPtr hInternet,
SET_OPTIONS option,
IntPtr buffer,
int bufferLength);
public enum SET_OPTIONS
{
INTERNET_OPTION_REFRESH = 37,
INTERNET_OPTION_SETTINGS_CHANGED = 39,
INTERNET_OPTION_PER_CONNECTION_OPTION = 75
};
private static void RefreshInternetExplorerSettings()
{
InternetSetOption(IntPtr.Zero, SET_OPTIONS.INTERNET_OPTION_SETTINGS_CHANGED, IntPtr.Zero, 0);
InternetSetOption(IntPtr.Zero, SET_OPTIONS.INTERNET_OPTION_REFRESH, IntPtr.Zero, 0);
}
精彩评论