これは私が見つけたコードです。 はキーワードは、私が行方不明になった重要な部分である待つ:
class CookieCollector : ICookieVisitor
{
private readonly TaskCompletionSource<List<Cookie>> _source = new TaskCompletionSource<List<Cookie>>();
public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie)
{
_cookies.Add(cookie);
if (count == (total - 1))
{
_source.SetResult(_cookies);
}
}
// https://github.com/amaitland/CefSharp.MinimalExample/blob/ce6e579ad77dc92be94c0129b4a101f85e2fd75b/CefSharp.MinimalExample.WinForms/ListCookieVisitor.cs
// CefSharp.MinimalExample.WinForms ListCookieVisitor
public Task<List<Cookie>> Task => _source.Task;
public static string GetCookieHeader(List<Cookie> cookies)
{
StringBuilder cookieString = new StringBuilder();
string delimiter = string.Empty;
foreach (var cookie in cookies)
{
cookieString.Append(delimiter);
cookieString.Append(cookie.Name);
cookieString.Append('=');
cookieString.Append(cookie.Value);
delimiter = "; ";
}
return cookieString.ToString();
}
private readonly List<Cookie> _cookies = new List<Cookie>();
public void Dispose()
{
}
}
// usage:
void foo()
{
var cookieManager = Cef.GetGlobalCookieManager();
var visitor = new CookieCollector();
cookieManager.VisitUrlCookies(targetUrl, true, visitor);
var cookies = await visitor.Task; // AWAIT !!!!!!!!!
var cookieHeader = CookieCollector.GetCookieHeader(cookies);
}
あなたは 'VisitAllCookiesAsync'方法で構築されたとの問題を経験していましたか? https://github.com/cefsharp/CefSharp/blob/11dee93bf331f012dd6416fb2f2162549d9137a3/CefSharp/ICookieManager.cs#L54 – amaitland