Files
KJD_WebService/TEMUKJD/ToolForms/Old/JDVerifyForm/CollectVerifyForm.cs
T

103 lines
2.8 KiB
C#

using AllBase;
using CefSharp;
using CefSharp.WinForms;
using TemuKJD.BaseForms;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TemuKJD.KJDForms.ToolForms.JDVerify
{
public partial class CollectVerifyForm : LRBaseForm
{
ChromiumWebBrowser cefWebBrowser1 = null;
public string url = "";
public CollectVerifyForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cefWebBrowser1 = new ChromiumWebBrowser(url);
cefWebBrowser1.BrowserSettings = new BrowserSettings { /*Plugins = CefState.Disabled*/ /*cef92*/ };
cefWebBrowser1.Dock = DockStyle.Fill;
panel1.Controls.Add(cefWebBrowser1);
cefWebBrowser1.FrameLoadEnd += CefWebBrowser1_FrameLoadEnd;
}
private void CefWebBrowser1_FrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
if (e.Url.Contains("item.m.jd.com/product"))
{
GetCookies();
Action action = new Action(() =>
{
Thread.Sleep(2000);
Work.CollectClass.JingXiCollect.VerifyCodeUrl = "";//验证之后,该值清空
this.Close();
});
Task.Factory.StartNew(() =>
{
if (this.InvokeRequired)
this.Invoke(action);
else
action();
});
}
}
/// <summary>
/// 获取cookies
/// </summary>
private void GetCookies()
{
cookies.Clear();
var cookieManager = Cef.GetGlobalCookieManager();
CookieVisitor visitor = new CookieVisitor();
visitor.SendCookie += visitor_SendCookie;
cookieManager.VisitAllCookies(visitor);
}
public List<CefSharp.Cookie> cookies = new List<CefSharp.Cookie>();
/// <summary>
/// 加载完成时,重设cookies
/// </summary>
/// <param name="obj"></param>
private void visitor_SendCookie(CefSharp.Cookie obj)
{
cookies.Add(obj);
Console.WriteLine(obj.Name + "=" + obj.Value);
}
}
public class CookieVisitor : CefSharp.ICookieVisitor
{
public event Action<CefSharp.Cookie> SendCookie;
public bool Visit(CefSharp.Cookie cookie, int count, int total, ref bool deleteCookie)
{
deleteCookie = false;
if (SendCookie != null)
{
SendCookie(cookie);
}
return true;
}
public void Dispose()
{
}
}
}