From 97e7a28c7d3a0dc8eda93222d8919cf7812a1ee8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=D0=A1=C2=B7=C3=A6?= <279202647@qq.com>
Date: Sun, 23 May 2021 19:53:03 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=94=99=E8=AF=AF=E6=8F=90?=
=?UTF-8?q?=E7=A4=BA?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../JdShopListener/JdShopListener.csproj | 1 +
.../JdShopListener/MainWindowViewModel.cs | 83 ++++++++++++++++++-
2 files changed, 81 insertions(+), 3 deletions(-)
diff --git a/JdShopListener/JdShopListener/JdShopListener.csproj b/JdShopListener/JdShopListener/JdShopListener.csproj
index c9d9233..1153a9f 100644
--- a/JdShopListener/JdShopListener/JdShopListener.csproj
+++ b/JdShopListener/JdShopListener/JdShopListener.csproj
@@ -21,6 +21,7 @@
+
diff --git a/JdShopListener/JdShopListener/MainWindowViewModel.cs b/JdShopListener/JdShopListener/MainWindowViewModel.cs
index 878dbc7..f2f243d 100644
--- a/JdShopListener/JdShopListener/MainWindowViewModel.cs
+++ b/JdShopListener/JdShopListener/MainWindowViewModel.cs
@@ -11,6 +11,7 @@ using System.Windows;
using System.Linq;
using System.Threading;
using Newtonsoft.Json.Linq;
+using PuppeteerSharp;
namespace JdShopListener
{
@@ -26,7 +27,7 @@ namespace JdShopListener
}
}
-
+ public string JDCookie { get; set; }
public MainWindowViewModel() {
ProList = new System.Collections.ObjectModel.ObservableCollection() {
new ProModel(){ Name="价格" , Type= ProType.Price},
@@ -231,6 +232,13 @@ namespace JdShopListener
{
try
{
+
+ if (string.IsNullOrEmpty(JDCookie))
+ {
+ AddLog("开始获取Cookie!");
+ InitLoginCookie();
+ }
+
var detail = GetItemDetail(Sku);
JArray list = detail.product.colorSize as JArray;
@@ -380,11 +388,19 @@ namespace JdShopListener
IsStart = true;
Thread t = new Thread(() =>
{
- LastItemChangeList.Clear();
- LastItemChangeList = DbHelper.Db.GetLastItemChanges();
while (IsStart==true)
{
+ if (string.IsNullOrEmpty(JDCookie))
+ {
+ AddLog("开始获取Cookie!");
+ InitLoginCookie();
+ }
+
+ LastItemChangeList.Clear();
+ LastItemChangeList = DbHelper.Db.GetLastItemChanges();
+
+
AddLog("开始sku定时监控!");
DoWork();
@@ -629,8 +645,18 @@ namespace JdShopListener
try
{
HttpClient http = new HttpClient();
+ http.DefaultRequestHeaders.Add("Cookie", JDCookie);
+ http.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0");
string html = http.GetStringAsync($"https://item.jd.com/{skuId}.html").Result;
+
+ if (html.Contains("passport.jd.com/new/login.aspx"))
+ {
+ AddLog("登录失效,触发验证!!重新尝试获取登录状态");
+ InitLoginCookie();
+ return GetItemDetail(skuId);
+ }
+
int start = html.IndexOf("var pageConfig");
int end = html.IndexOf("};", start);
@@ -839,7 +865,19 @@ namespace JdShopListener
string url = $"https://item-soa.jd.com/getWareBusiness?callback=jQuery6834366&skuId={model.SkuId}&cat={model.Cat}&area=19_1666_1669_0&shopId={model.ShopId}&venderId={model.VenderId}¶mJson=%7B%22platform2%22%3A%221%22%2C%22colType%22%3A0%2C%22specialAttrStr%22%3A%22p0ppp1pppppp3ppppppppppp%22%2C%22skuMarkStr%22%3A%2200%22%7D&num=1";
HttpClient client = new HttpClient();
+
+ client.DefaultRequestHeaders.Add("Cookie", JDCookie);
+ client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36 SE 2.X MetaSr 1.0");
+
var json= client.GetStringAsync(url).Result;
+
+ if (json.Contains("passport.jd.com/new/login.aspx"))
+ {
+ AddLog("登录失效,触发验证!!重新尝试获取登录状态");
+ InitLoginCookie();
+ return GetWareBusiness(model);
+ }
+
json = json.Remove(0, "jQuery6834366(".Length);
json = json.Remove(json.Length - 1, 1);
@@ -859,6 +897,45 @@ namespace JdShopListener
return date;
}
+
+
+
+ ///
+ /// 刷新Cookie
+ ///
+ private void InitLoginCookie()
+ {
+
+ // 下载浏览器执行程序
+ // var fetcher= new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultRevision).Result;
+
+ string url = "https://passport.jd.com/uc/login";
+ // 创建一个浏览器执行实例
+ using var browser = Puppeteer.LaunchAsync(new LaunchOptions
+ {
+ Headless = true,
+
+ //Args = new string[] { "--no-sandbox" }
+ }).Result;
+ // 打开一个页面
+ var page = browser.NewPageAsync().Result;
+
+
+ var a = page.GoToAsync(url, WaitUntilNavigation.Networkidle0).Result;
+ var content = page.GetContentAsync().Result;
+ page.DeleteCookieAsync().Wait();
+ var cookies = page.GetCookiesAsync().Result;
+
+ JDCookie = string.Empty;
+
+ cookies.ToList().ForEach(c =>
+ {
+ JDCookie += $"{c.Name}={c.Value};";
+ });
+
+
+ }
+
}