开发者

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

开发者 https://www.devze.com 2023-02-09 02:56 出处:网络
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

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 Setting开发者_StackOverflow中文版s", 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();


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消