using CefSharp; using System; using System.Collections.Generic; using System.Linq; using System.Security.Cryptography.X509Certificates; using System.Text; using System.Threading.Tasks; namespace WindowsFormsApp1.utils.tb { class TBLoginRequestHandler : IRequestHandler { private String mark; private IGetTBCookieListener tbCookieListener; public TBLoginRequestHandler(String mark, IGetTBCookieListener tbCookieListener) { this.mark = mark; this.tbCookieListener = tbCookieListener; } public class CookieVisitor : ICookieVisitor { private Dictionary cookiesMap = new Dictionary(); private IGetTBCookieListener tbCookieListener; public CookieVisitor() { } public void setTBCookieListener(IGetTBCookieListener tbCookieListener) { this.tbCookieListener = tbCookieListener; } public void Dispose() { } public bool Visit(Cookie cookie, int count, int total, ref bool deleteCookie) { cookiesMap.Add(cookie.Name, cookie.Value); if (count + 1 >= total) this.success(); Console.WriteLine(count + ":" + total); return true; } //cookies获取成功 private void success() { String cookies = ""; StringBuilder sb = new StringBuilder(""); foreach (KeyValuePair kvp in cookiesMap) { sb.Append(kvp.Key + "=" + kvp.Value + ";"); } cookies = sb.ToString(); if (cookies.EndsWith(";")) cookies = cookies.Substring(0, cookies.Length - 1); Console.WriteLine(cookies); if (cookiesMap.ContainsKey("login")) tbCookieListener.onSuccess(cookiesMap, cookies); } } public IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling) { if (request.Url.StartsWith(this.mark)) { Console.WriteLine("cookie开始"); CookieVisitor cookieVisitor = new CookieVisitor(); cookieVisitor.setTBCookieListener(tbCookieListener); chromiumWebBrowser.GetCookieManager().VisitUrlCookies(request.Url, true, cookieVisitor); Console.WriteLine("cookie结束"); } return null; } public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect) { return false; } public bool OnCertificateError(IWebBrowser chromiumWebBrowser, IBrowser browser, CefErrorCode errorCode, string requestUrl, ISslInfo sslInfo, IRequestCallback callback) { return true; } public bool OnOpenUrlFromTab(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, string targetUrl, WindowOpenDisposition targetDisposition, bool userGesture) { return false; } public void OnPluginCrashed(IWebBrowser chromiumWebBrowser, IBrowser browser, string pluginPath) { } public bool OnQuotaRequest(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, long newSize, IRequestCallback callback) { return true; } public void OnRenderProcessTerminated(IWebBrowser chromiumWebBrowser, IBrowser browser, CefTerminationStatus status) { } public void OnRenderViewReady(IWebBrowser chromiumWebBrowser, IBrowser browser) { } public bool OnSelectClientCertificate(IWebBrowser chromiumWebBrowser, IBrowser browser, bool isProxy, string host, int port, X509Certificate2Collection certificates, ISelectClientCertificateCallback callback) { return true; } bool GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { return true; } bool IRequestHandler.GetAuthCredentials(IWebBrowser chromiumWebBrowser, IBrowser browser, string originUrl, bool isProxy, string host, int port, string realm, string scheme, IAuthCallback callback) { return true; } } public interface IGetTBCookieListener { void onSuccess(Dictionary cookiesMap, String cookies); } }