C# 使用Watin更改IE实例的代理设置
我知道我可以更改计算机全局代理设置,C# 使用Watin更改IE实例的代理设置,c#,asp.net,unit-testing,watin,C#,Asp.net,Unit Testing,Watin,我知道我可以更改计算机全局代理设置,Software\\Microsoft\\Windows\\CurrentVersion\\Internet设置,以影响使用创建的IE实例 但是有没有办法拦截IE浏览器发出的请求并通过代理运行它们呢?我的目标是运行多个IE实例,每个实例都有自己的代理,这在我上面的当前解决方案中是不可能的。使用IE甚至WebBrowser都不可能做到这一点(它只是IE的一个实例) 但您可以通过操纵WebBrowser行为来实现所需的功能 可以编写自定义WebBrowser,通过
Software\\Microsoft\\Windows\\CurrentVersion\\Internet设置
,以影响使用创建的IE实例
但是有没有办法拦截IE浏览器发出的请求并通过代理运行它们呢?我的目标是运行多个IE实例,每个实例都有自己的代理,这在我上面的当前解决方案中是不可能的。使用IE甚至WebBrowser都不可能做到这一点(它只是IE的一个实例) 但您可以通过操纵WebBrowser行为来实现所需的功能 可以编写自定义WebBrowser,通过发送包含不同代理的自定义WebRequest来获取数据
我知道您正在寻找一种不使用计算机全局代理设置的替代解决方案,但我想在此处添加此设置,以便其他没有此限制的人了解它 答案就在你的问题上- 在运行时全局更改代理设置很简单,您需要使用
Microsoft.Win32
命名空间中的Microsoft.Win32.registry
类更改您感兴趣的注册表项
您可以在此处找到有关此的MSDN文档:
请参见下面的示例,了解如何执行此操作
RegistryKey myKey = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Your key", true);
myKey.SetValue("My String Value", "Test Value", RegistryValueKind.String);
现在,要更改框上的代理设置,您需要更改或创建正确的代理注册表项,您可以在以下位置找到所有可用项:
- HKEY\ U当前\用户\软件\ Microsoft\Windows\CurrentVersion\Internet设置\代理启用
- HKEY\ U当前\用户\软件\ Microsoft\Windows\CurrentVersion\Internet设置\ProxyServer
- HKEY\ U当前\用户\软件\ Microsoft\Windows\CurrentVersion\Internet设置\ProxyOverride
- HKEY\ U当前\用户\软件\ Microsoft\Windows\CurrentVersion\Internet设置\HTTP1\ 1通过代理
WatinUser
的用户设置代理设置,您可以使用类自动创建此用户
- 参见SO中的示例:
- 有这样的产品,您可以根据应用程序名称、IP地址、主机名和端口号设置规则,将流量路由到不同的代理。这不允许您对多个IE进程使用不同的代理,但如果这些进程访问不同的URL,您可以通过单独的代理服务器路由流量。Proxifier使用WinSocks堆栈工作,类似于许多防病毒软件使用的堆栈,并且对应用程序层是透明的。我创建了一个名为
Process Proxifier
的应用程序,它使用FiddlerCore向Windows应用程序动态添加代理设置。您可以在此处找到其完整源代码:
我还应该提到,此解决方案仅限于具有系统默认“CERN”代理设置(指向Fiddler/FiddlerCore)的目标进程 另一个建议是编写自己的web请求拦截器/代理服务器,从请求的url获取代理服务器信息,并将规范化的url转发到真正的代理服务器。 例如,从watin启动url“someurl?ProxyServer=10.10.10.12”,现在这将被您自己的代理服务器截获,它将使用代理服务器参数将请求的url(即“someurl”)重定向到10.10.10.12。您的代理服务器实现可以在运行时设置代理详细信息,并使用动态代理从服务器获取结果
我希望它有一定的意义。watinie创建多个进程id(单实例IE创建多个进程id)。为了通过使用Fiddler Core覆盖WatiN的代理设置,我们需要获取WatiN IE创建的所有子进程ID。可以在该类中找到Helper类。然后我们检查BeforeRequest事件中的所有进程id,并等待watin进程id覆盖代理设置
private void FiddlerApplication_BeforeRequest(Session sess)
{
//Debug.WriteLine("FiddlerApplication_BeforeRequest: " + sess.LocalProcessID.ToString());
if (WatinIEprocessHolder.ContainsKey(sess.LocalProcessID))
{
//see http://stackoverflow.com/questions/14284256/how-to-manually-set-upstream-proxy-for-fiddler-core
sess["X-OverrideGateway"] = WatinIEprocessHolder[sess.LocalProcessID];
}
}
工作测试应用程序可在此下载
下面是使用不同匿名代理的测试结果。(ipaddress=browser.Text)
示例代码:
private void this_FormClosing(object sender, FormClosingEventArgs e)
{
StopFiddler();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += this_FormClosing;
ProxyHolder = new List<string>();
ProxyHolder.Add("119.46.110.17:8080");
ProxyHolder.Add("178.21.112.27:3128");
ProxyHolder.Add("122.96.59.107:83");
ProxyHolder.Add("136.0.16.217:3127");
ProxyHolder.Add("198.52.199.152:7808");
ProxyHolder.Add("122.96.59.107:82");
StartFiddler();
System.Threading.Thread.Sleep(500);
for (var i = 0; i < ProxyHolder.Count; i++)
{
WhatIsMyIpThroughProxy(ProxyHolder[i]);
Application.DoEvents();
System.Threading.Thread.Sleep(500);
}
//WhatIsMyIpThroughProxy();
}
private Dictionary<int, string> WatinIEprocessHolder = new Dictionary<int, string>();
private List<string> ProxyHolder = null;
public void WhatIsMyIpThroughProxy(string ProxyIPandPort)
{
using (var browser = new IE(true))// we should not navigate now. Because we need process ids.
{
WindowHandleInfo ChildHandles = new WindowHandleInfo(browser.hWnd);
foreach (var cHandle in ChildHandles.GetAllChildHandles())
{
int pid = new WatiN.Core.Native.Windows.Window(cHandle).ProcessID;
if (WatinIEprocessHolder.ContainsKey(pid) == false)
WatinIEprocessHolder.Add(pid, ProxyIPandPort);
}
System.Text.StringBuilder processIDs = new System.Text.StringBuilder();
foreach (var k in WatinIEprocessHolder.Keys)
{
processIDs.Append(k.ToString() + ",");
//Debug.WriteLine(string.Format("{0}:{1}", k, WatinIEprocessHolder[k]));
}
//we got the process ids above. Navigate now.
browser.GoTo("http://www.rentanadviser.com/en/common/tools.ashx?action=whatismyip");
browser.WaitForComplete();
WatinIEprocessHolder.Clear();
System.Net.IPAddress ip;
if (System.Net.IPAddress.TryParse(browser.Text, out ip))
{
Debug.WriteLine(string.Format("Process Ids:{0}, Your IP address: {1}, Proxy:{2}", processIDs.ToString(), browser.Text, ProxyIPandPort));
}
else
{
Debug.WriteLine(string.Format("Process Ids:{0}, Your IP address: {1}, Proxy:{2}", processIDs.ToString(), "Failed", ProxyIPandPort));
}
}
}
private void StartFiddler()
{
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
}
private void StopFiddler()
{
FiddlerApplication.BeforeRequest -= FiddlerApplication_BeforeRequest;
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
private void FiddlerApplication_BeforeRequest(Session sess)
{
//Debug.WriteLine("FiddlerApplication_BeforeRequest: " + sess.LocalProcessID.ToString());
if (WatinIEprocessHolder.ContainsKey(sess.LocalProcessID))
{
//see http://stackoverflow.com/questions/14284256/how-to-manually-set-upstream-proxy-for-fiddler-core
sess["X-OverrideGateway"] = WatinIEprocessHolder[sess.LocalProcessID];
}
}
private void this\u FormClosing(对象发送方,FormClosingEventArgs e)
{
StopFiddler();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
this.FormClosing+=此_FormClosing;
ProxyHolder=新列表();
代理持有人添加(“119.46.110.17:8080”);
代理持有人添加(“178.21.112.27:3128”);
代理持有人添加(“122.96.59.107:83”);
代理持有人。添加(“136.0.16.217:3127”);
代理持有人添加(“198.52.199.152:7808”);
代理持有人添加(“122.96.59.107:82”);
StartFiddler();
系统.线程.线程.睡眠(500);
对于(变量i=0;iProcess Ids:3852,7852,, Your IP address: 119.46.110.17, Proxy:119.46.110.17:8080
Process Ids:2508,6948,, Your IP address: 178.21.112.27, Proxy:178.21.112.27:3128
Process Ids:1348,1368,, Your IP address: 122.96.59.107, Proxy:122.96.59.107:83
Process Ids:7152,5104,, Your IP address: 136.0.16.217, Proxy:136.0.16.217:3127
Process Ids:4128,3480,, Your IP address: 198.52.199.152, Proxy:198.52.199.152:7808
Process Ids:2036,7844,, Your IP address: 122.96.59.107, Proxy:122.96.59.107:82
private void this_FormClosing(object sender, FormClosingEventArgs e)
{
StopFiddler();
}
private void Form1_Load(object sender, EventArgs e)
{
this.FormClosing += this_FormClosing;
ProxyHolder = new List<string>();
ProxyHolder.Add("119.46.110.17:8080");
ProxyHolder.Add("178.21.112.27:3128");
ProxyHolder.Add("122.96.59.107:83");
ProxyHolder.Add("136.0.16.217:3127");
ProxyHolder.Add("198.52.199.152:7808");
ProxyHolder.Add("122.96.59.107:82");
StartFiddler();
System.Threading.Thread.Sleep(500);
for (var i = 0; i < ProxyHolder.Count; i++)
{
WhatIsMyIpThroughProxy(ProxyHolder[i]);
Application.DoEvents();
System.Threading.Thread.Sleep(500);
}
//WhatIsMyIpThroughProxy();
}
private Dictionary<int, string> WatinIEprocessHolder = new Dictionary<int, string>();
private List<string> ProxyHolder = null;
public void WhatIsMyIpThroughProxy(string ProxyIPandPort)
{
using (var browser = new IE(true))// we should not navigate now. Because we need process ids.
{
WindowHandleInfo ChildHandles = new WindowHandleInfo(browser.hWnd);
foreach (var cHandle in ChildHandles.GetAllChildHandles())
{
int pid = new WatiN.Core.Native.Windows.Window(cHandle).ProcessID;
if (WatinIEprocessHolder.ContainsKey(pid) == false)
WatinIEprocessHolder.Add(pid, ProxyIPandPort);
}
System.Text.StringBuilder processIDs = new System.Text.StringBuilder();
foreach (var k in WatinIEprocessHolder.Keys)
{
processIDs.Append(k.ToString() + ",");
//Debug.WriteLine(string.Format("{0}:{1}", k, WatinIEprocessHolder[k]));
}
//we got the process ids above. Navigate now.
browser.GoTo("http://www.rentanadviser.com/en/common/tools.ashx?action=whatismyip");
browser.WaitForComplete();
WatinIEprocessHolder.Clear();
System.Net.IPAddress ip;
if (System.Net.IPAddress.TryParse(browser.Text, out ip))
{
Debug.WriteLine(string.Format("Process Ids:{0}, Your IP address: {1}, Proxy:{2}", processIDs.ToString(), browser.Text, ProxyIPandPort));
}
else
{
Debug.WriteLine(string.Format("Process Ids:{0}, Your IP address: {1}, Proxy:{2}", processIDs.ToString(), "Failed", ProxyIPandPort));
}
}
}
private void StartFiddler()
{
FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest;
FiddlerApplication.Startup(8888, true, true, true);
}
private void StopFiddler()
{
FiddlerApplication.BeforeRequest -= FiddlerApplication_BeforeRequest;
if (FiddlerApplication.IsStarted())
{
FiddlerApplication.Shutdown();
}
}
private void FiddlerApplication_BeforeRequest(Session sess)
{
//Debug.WriteLine("FiddlerApplication_BeforeRequest: " + sess.LocalProcessID.ToString());
if (WatinIEprocessHolder.ContainsKey(sess.LocalProcessID))
{
//see http://stackoverflow.com/questions/14284256/how-to-manually-set-upstream-proxy-for-fiddler-core
sess["X-OverrideGateway"] = WatinIEprocessHolder[sess.LocalProcessID];
}
}