每个浏览器单独缓存?
Separate cache per browser?
目前我设置缓存路径如下:
CefSettings settings = new CefSettings();
settings.CachePath = mycachePath;
Cef.Initialize(settings);
var browser = new ChromiumWebBrowser(myUrl);
以上有效。
但是,我需要同时使用 2 个不同的帐户登录网站,但它使用相同的 cookie 容器。因此,如果我使用一个帐户登录,然后使用另一个帐户登录,则第一个帐户将被覆盖。
是否可以为每个浏览器设置一个缓存路径?
或者有更好的方法来处理这种情况吗?
您似乎在使用 CefSharp? If so, looking through the code, it seems that you want to create the browser with an empty 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))
{
...
}
这是旧的,但我刚看到它,它需要一个更完整的答案。您可以根据需要打开任意数量的浏览器实例,每个实例都有自己独立的缓存和 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
目前我设置缓存路径如下:
CefSettings settings = new CefSettings();
settings.CachePath = mycachePath;
Cef.Initialize(settings);
var browser = new ChromiumWebBrowser(myUrl);
以上有效。
但是,我需要同时使用 2 个不同的帐户登录网站,但它使用相同的 cookie 容器。因此,如果我使用一个帐户登录,然后使用另一个帐户登录,则第一个帐户将被覆盖。
是否可以为每个浏览器设置一个缓存路径?
或者有更好的方法来处理这种情况吗?
您似乎在使用 CefSharp? If so, looking through the code, it seems that you want to create the browser with an empty 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))
{
...
}
这是旧的,但我刚看到它,它需要一个更完整的答案。您可以根据需要打开任意数量的浏览器实例,每个实例都有自己独立的缓存和 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