using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using WindowsFormsApp1.entity.tb; namespace WindowsFormsApp1.utils.tb { class SDLJGoodsManager { private TBAccountLogin tbAccountLogin; private String baseUrl; private int position; private void uploadContentSuccess(String result) { JObject root = JObject.Parse(result); if (Convert.ToInt32(root["code"]) == 0) { Random ra = new Random(); //继续爬下一页 Thread.Sleep(1000 + ra.Next(2000));//间隔1s到3s StartRequestGoods(this.baseUrl, position); } else if (Convert.ToInt32(root["code"]) == 1001) { //授权出错 TBCookieUtil.InvalidLogin(position); } } private void getRequestUrlSuccess(String result) { JObject root= JObject.Parse(result); if (Convert.ToInt32(root["code"]) == 0) { String url= root["data"]["url"].ToString(); this.baseUrl = url; Dictionary headerMap = new Dictionary(); headerMap.Add("cookie", tbAccountLogin.Cookie); result = HttpUtil.HttpGet(url,headerMap); OnSuccess listener = uploadContentSuccess; ApiUtil.uploadContent( tbAccountLogin.TbUid, result, listener); } } //启动爬虫 public void StartRequestGoods(String baseUrl,int position) { if (!Constant.tbAccountMap.ContainsKey(position) || !Constant.tbAccountMap[position].Login) return; this.position = position; this.tbAccountLogin = Constant.tbAccountMap[position]; OnSuccess listener = getRequestUrlSuccess; ApiUtil.getRequestUrl( tbAccountLogin.TbUid, baseUrl, listener); } } }