带有CefSharp Offscreen的仅一个浏览器的代理 [英] Proxy for only one browser with CefSharp Offscreen
问题描述
我试图为 ChromiumWebBrowser()
设置代理,而不更改其他浏览器的设置。
I am trying to set a proxy for a ChromiumWebBrowser()
without changing the settings of other browsers.
我的代码如下:
CEF初始化
在这里我将进行初始化CefSharp并调用将测试以设置代理的方法
Here I will initialize CefSharp and call the method that will test to set the proxy
public CFTryOut()
{
var settings = new CefSettings()
{
CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache"),
};
CefSharpSettings.ShutdownOnExit = true;
Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);
ProxyTest();
}
ProxyTest
在这里,我想创建两个 ChromiumWebBrowser()
并将代理设置为仅其中之一
Here I want to create two ChromiumWebBrowser()
and set a proxy to only one of them
async Task ProxyTest()
{
ChromiumWebBrowser firstbrowser = new ChromiumWebBrowser();
ChromiumWebBrowser secondbrowser = new ChromiumWebBrowser();
waitini:
if (!firstbrowser.IsBrowserInitialized && !secondbrowser.IsBrowserInitialized)
{
Thread.Sleep(100);
goto waitini;
}
firstbrowser.LoadingStateChanged += FirstBrowserLoadingStateChanged;
secondbrowser.LoadingStateChanged += SecondBrowserLoadingStateChanged;
OpenSync("http://icanhazip.com/", firstbrowser);
string x = await firstbrowser.GetBrowser().MainFrame.GetSourceAsync();
//Set the Proxy
await Cef.UIThreadTaskFactory.StartNew(delegate
{
var rc = firstbrowser.GetBrowser().GetHost().RequestContext;
var v = new Dictionary<string, object>();
v["mode"] = "fixed_servers";
v["server"] = "http://45.77.248.104:8888";
string error;
bool success = rc.SetPreference("proxy", v, out error);
});
OpenSync("http://icanhazip.com/", firstbrowser);
string y = await firstbrowser.GetBrowser().MainFrame.GetSourceAsync();
OpenSync("http://icanhazip.com/", secondbrowser);
string z = await secondbrowser.GetBrowser().MainFrame.GetSourceAsync();
}
在这里, First / SecondBrowserLoadingStateChanged
允许我在页面加载完成时进行标记,以使 OpenSync
等待页面加载结束,然后返回:
Here, the First/SecondBrowserLoadingStateChanged
allow me to flag when the page loading is finished in order for OpenSync
to wait for page loading to end before returning :
public void OpenSync(string url, ChromiumWebBrowser browser)
{
IsLoading = true;
browser.Load(url);
SpinWait.SpinUntil(() => !IsLoading);
}
我期望
x =我的ip-xx.xx.xx.xx
x = my ip - xx.xx.xx.xx
y =代理的ip-45.77.248.104
y = proxy's ip - 45.77.248.104
z =我的IP-xx.xx.xx.xx
z = my ip - xx.xx.xx.xx
我得到了
x =我的ip-xx.xx.xx.xx
x = my ip - xx.xx.xx.xx
y =代理的ip-45.77.248.104
y = proxy's ip - 45.77.248.104
z =代理的ip-45.77.248.104
z = proxy's ip - 45.77.248.104
问题是我没有在 secondbrowser
,但请求通过代理。我猜那是因为它们共享同一主机。
The thing is I did not set any proxy on the secondbrowser
yet the request goes through the proxy. I guess that's because they share the same host.
所以
1)我如何指定每个 ChromiumWebBrowser
的专用代理?
1) how can I specify a dedicated proxy for each ChromiumWebBrowser
?
或
2)如何为每个新的 ChromiumWebBrowser
指定不同的主机?
2) how can I specify a different host for each new ChromiumWebBrowser
?
推荐答案
实际上,这非常容易。
我的问题是我试图设置RequestContext在浏览器初始化之后,它是只读的。
My problem was that I was trying to set the RequestContext after the browser initialization, while it's read only.
但是它可以在构造函数中作为参数传递:
But it can be passed as a parameter in the constructor :
var rc1 = new RequestContext();
ChromiumWebBrowser firstbrowser = new ChromiumWebBrowser("", null, rc1);
var rc2 = new RequestContext();
ChromiumWebBrowser secondbrowser = new ChromiumWebBrowser("", null, rc2);
对Amaitland的所有信用
All credit to Amaitland
对于更可重用的方式是,可以像这样将代理设置过程逐出:
For a more reusable way, the process to set the proxy can be deported like this :
async private Task SetProxy(ChromiumWebBrowser cwb, string Address)
{
await Cef.UIThreadTaskFactory.StartNew(delegate
{
var rc = cwb.GetBrowser().GetHost().RequestContext;
var v = new Dictionary<string, object>();
v["mode"] = "fixed_servers";
v["server"] = Address;
string error;
bool success = rc.SetPreference("proxy", v, out error);
});
}
然后我可以打电话:
SetProxy(firstbrowser, "123.123.123.123:1234")
这篇关于带有CefSharp Offscreen的仅一个浏览器的代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!