目前我按如下方式设置缓存路径:
CefSettings settings = new CefSettings();
settings.CachePath = mycachePath;
Cef.Initialize(settings);
var browser = new ChromiumWebBrowser(myUrl);
以上作品。
但是,我需要同时登录一个包含2个不同帐户的网站,但它使用相同的cookie容器。因此,如果我使用一个帐户然后另一个帐户登录,则会覆盖第一个帐户。
是否可以为每个浏览器设置一个缓存路径?
或者有更好的方法来处理这种情况吗?
答案 0 :(得分:4)
您好像正在使用CefSharp?如果是这样,查看代码,您似乎想要创建一个空CachePath的浏览器:
/// <summary>
/// Returns the cache path for this object. If empty an "incognito mode"
/// in-memory cache is being used.
/// </summary>
string CachePath { get; }
看着他们的sample(我假设没有窗户),看起来它会得到你想要的大致:
var browserSettings = new BrowserSettings();
var requestContextSettings = new RequestContextSettings { CachePath = "" };
using(var requestContext = new RequestContext(requestContextSettings))
using (var browser = new ChromiumWebBrowser(TestUrl, browserSettings, requestContext))
{
...
}
答案 1 :(得分:1)
这是旧的,但是我刚遇到它,它需要一个更完整的答案。您可以根据需要打开任意数量的浏览器实例,每个实例都有自己独立的缓存和cookie,它们彼此独立。您要做的就是为每个浏览器设置CachePath
设置属性,确保其路径是唯一的,然后创建浏览器。
您可能会在其中使用选项卡的示例场景,其中Tab1具有Browser1,Tab2具有Browser2,依此类推,而每个浏览器实例都不了解其他实例。这是通过在创建每个浏览器之前为其分配自己的缓存路径来实现的。
在VB .NET中:
CEFPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\My\Special\Cache\Path"
If Not Directory.Exists(CEFPath) Then
Try
Directory.CreateDirectory(CEFPath)
Catch ex As Exception
MsgBox("Error creating cache directory" + vbCrLf + CEFPath,, "Error")
End Try
End If
Dim settings As New CefSettings()
settings.CachePath = CEFPath
'Settings.Proxy = new ProxyOptions(ip: "myipaddress", port: "myport", username: "myusername", password: "mypassword")
' initialization before creating instance
If CefSharp.Cef.IsInitialized = False Then
CefSharp.Cef.Initialize(settings)
End If
browser = New ChromiumWebBrowser("")
Dim requestContextSettings As New RequestContextSettings()
requestContextSettings.CachePath = CEFPath
'Optional:
requestContextSettings.PersistSessionCookies = True
'https://github.com/cefsharp/CefSharp/wiki/General-Usage
browser.RequestContext = New RequestContext(requestContextSettings)
我正在使用NuGet软件包v83.4.20