Change proxy of IE c# for win 7 64 bit(test using httperbrequest to make sure registry change)

南楼画角 提交于 2019-12-12 06:31:09

问题


I have been testing my application on 32 bit and it is working fine but as soon it switches to 64 bit OS then there is a problem

it changes proxy for one time but never again it keeps and tests same proxy for the rest of the loop

.I came across some posts that shows that there is some difference bit 32 and 64 bit registry and have written a code but it dose not work well on 64 bit environment

[DllImport("kernel32.dll", SetLastError = true, CallingConvention = CallingConvention.Winapi)] 


[return: MarshalAs(UnmanagedType.Bool)] 
public static extern bool IsWow64Process([In] IntPtr hProcess, [Out] out bool lpSystemInfo);

public bool Is64Bit()
{
    bool retVal;

    IsWow64Process(System.Diagnostics.Process.GetCurrentProcess().Handle, out retVal);

    return retVal;
} 

Microsoft.Win32.RegistryKey registry;
if (Is64Bit())
{
    MessageBox.Show("Oh HO! you are running 64 bit version");
     registry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Wow6432Node\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
}
else
{
    MessageBox.Show("Yes it is 32 bit");
     registry = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", true);
}

string orignalProxyServer = registry.GetValue("ProxyServer").ToString();
int orignalProxyEnable = int.Parse(registry.GetValue("ProxyEnable").ToString());
int i = 0;

while (i < listBox1.Items.Count)
{

    //Enter new proxies Settings//
    registry.SetValue("ProxyEnable", 1);
    registry.SetValue("ProxyServer", listBox1.Items[i].ToString());
    RefreshInternetExplorerSettings();

    try
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.whatismyip.com/automation/n09230945.asp");
        using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
        {
            resopnse = reader.ReadToEnd();
            this.Invoke(new EventHandler(DoIe));                       

        }
    }
    catch (Exception c)
    {
        MessageBox.Show(c.Message);

    }
    i++;
    Application.DoEvents();
    backgroundWorker1.ReportProgress(i);
}
registry.SetValue("ProxyEnable", orignalProxyEnable);
registry.SetValue("ProxyServer", orignalProxyServer);
RefreshInternetExplorerSettings();

回答1:


It seems that there is quite a dependency on the version of Internet Explorer and the registry nodes it's using:

For Windows x86-based computers:

To change the machine-wide connection attempts: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet Settings

To change the user-specific connection attempts: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

For Windows x64-based computers:

To change the machine-wide connection attempts for Internet Explorer 32-bit: HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings

To change the user-specific connection attempts for Internet Explorer 32-bit: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

To change the machine-wide connection attempts for Internet Explorer 64-bit: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Internet Settings

To change the user-specific connection attempts for Internet Explorer 64-bit: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings

Source: http://www.thewindowsclub.com/fix-internet-explorer-cannot-display-the-webpage#ixzz1DPF9FSvK

I would recommend you to try and use the same node for x86 and x64 systems.




回答2:


If you need to set the proxy only for the HttpWebRequest, you could do

req.Proxy = new WebProxy(listBox1.Items[i].ToString());

instead of changing the registry settings.



来源:https://stackoverflow.com/questions/4937784/change-proxy-of-ie-c-sharp-for-win-7-64-bittest-using-httperbrequest-to-make-su

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!