admin
2020-06-17 9d3d08ba960fc739498b0648d57eaf2c50a40fd1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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<String, String> headerMap = new Dictionary<String, String>();
                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);
        }
 
    }
}