Пример #1
0
 public GetCourseList(string id, string password, Action <string> logDelegate)
 {
     _httpHelper   = new HTTPHelper(5000);
     _jpgreader    = new Readjpg();
     _userId       = id;
     _userPassword = password;
     _logDelegate  = logDelegate;
 }
Пример #2
0
 public GetCourseList(string id, string password, Action<string> logDelegate)
 {
     _httpHelper = new HTTPHelper(5000);
     _jpgreader = new Readjpg();
     _userId = id;
     _userPassword = password;
     _logDelegate = logDelegate;
 }
Пример #3
0
        public List <CourseRemainCapacity> GetCourseRemainCapacity(Semester semester)
        {
            string html;
            var    rand = new Random(5);

            var courseList = new List <CourseRemainCapacity>();

            {
                var url     = "http://zhjwxk.cic.tsinghua.edu.cn/xkBks.vxkBksXkbBs.do?m=xkqkSearch&p_xnxq=" + semester.Id;
                var request = _httpHelper.CreateHTTPGetRequest(url, true);
                html = HTTPHelper.GetResponseString(request);
            }

            courseList.AddRange(ParseCourseRemainCapacity(html));

            var token     = getToken(html);
            var match     = Regex.Match(html, "第 1 页 / 共 (\\d+) 页");
            int pageCount = int.Parse(match.Groups[1].ToString());

            OutputLog(string.Format("共计 {0} 页", pageCount));

            OutputLog("成功抓取第 1 页");

            for (int i = 2; i <= pageCount; i++)
            {
                System.Threading.Thread.Sleep(500 + rand.Next(0, 1000));

                var url     = "http://zhjwxk.cic.tsinghua.edu.cn/xkBks.vxkBksJxjhBs.do";
                var postStr = string.Format("m=kylSearch&page={2}&token={0}&p_sort.p1=&p_sort.p2=&p_sort.asc1=&p_sort.asc2=&p_xnxq={1}&pathContent=课余量查询&p_kch=&p_kxh=&p_kcm=&p_skxq=&p_skjc=&goPageNumber={3}",
                                            token, semester.Id, i, i - 1);

                var request = _httpHelper.CreateHTTPPOSTRequest(url, postStr, true);
                html = HTTPHelper.GetResponseString(request);

                OutputLog(string.Format("成功抓取第 {0} 页", i));

                courseList.AddRange(ParseCourseRemainCapacity(html));
            }

            return(courseList);
        }
Пример #4
0
        public List <CourseRawData> GetCourses(Semester semester)
        {
            string html;
            var    rand = new Random(5);

            var courseList = new List <CourseRawData>();

            {
                var url     = "http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsJxjhBs.do?m=kkxxSearch&p_xnxq=" + semester.Id;
                var request = _httpHelper.CreateHTTPGetRequest(url, true);
                html = HTTPHelper.GetResponseString(request);
            }

            courseList.AddRange(ParseCourse(html));

            var token     = getToken(html);
            var match     = Regex.Match(html, "第 1 页 / 共 (\\d+) 页");
            var pageCount = int.Parse(match.Groups[1].ToString());

            OutputLog(string.Format("共计 {0} 页", pageCount));

            OutputLog("成功抓取第 1 页");

            for (int i = 2; i <= pageCount; i++)
            {
                System.Threading.Thread.Sleep(500 + rand.Next(0, 1000));

                var url     = "http://zhjwxk.cic.tsinghua.edu.cn/xkYjs.vxkYjsJxjhBs.do";
                var postStr = string.Format("m=kkxxSearch&page={2}&token={0}&p_sort.p1=&p_sort.p2=&p_sort.asc1=true&p_sort.asc2=true&p_xnxq={1}&pathContent=&showtitle=&p_kkdwnm=&p_kch=&p_kcm=&p_zjjsxm=&p_kcflm=&p_skxq=&p_skjc=&p_xkwzsm=&p_kctsm=&p_kctsm_new_value=false&p_ssnj=&p_rxklxm=&goPageNumber={3}",
                                            token, semester.Id, i, i - 1);

                var request = _httpHelper.CreateHTTPPOSTRequest(url, postStr, true);
                html = HTTPHelper.GetResponseString(request);

                OutputLog(string.Format("成功抓取第 {0} 页", i));

                courseList.AddRange(ParseCourse(html));
            }

            return(courseList);
        }
Пример #5
0
        public void Login()
        {
            OutputLog("开始登录");

            {
                var loginUrl1 = "http://zhjwxk.cic.tsinghua.edu.cn/xsxk_index.jsp";
                var request   = _httpHelper.CreateHTTPGetRequest(loginUrl1, true);
                HTTPHelper.GetResponseString(request);
            }

            {
                var loginUrl2 = "http://zhjwxk.cic.tsinghua.edu.cn/xklogin.do";
                var request   = _httpHelper.CreateHTTPGetRequest(loginUrl2, true);
                HTTPHelper.GetResponseString(request);
            }

            var login      = false;
            int errorcount = 0;

            while (login == false && errorcount < 5)
            {
                string jpgcode = null;
                OutputLog("获取jpg");
                while (jpgcode == null)
                {
                    var loginUrl3 = "http://zhjwxk.cic.tsinghua.edu.cn/login-jcaptcah.jpg?captchaflag=login1";
                    var request   = _httpHelper.CreateHTTPGetRequest(loginUrl3, true);
                    jpgcode = HTTPHelper.GetResponseJpgCode(request, _jpgreader);

                    if (jpgcode == null)
                    {
                        OutputLog("识别jpg失败");
                    }
                    System.Threading.Thread.Sleep(1000);
                }

                OutputLog("开始HTTPS链接");
                {
                    var loginUrl4 = "https://zhjwxk.cic.tsinghua.edu.cn:443/j_acegi_formlogin_xsxk.do";
                    var postStr   = string.Format("j_username={0}&j_password={1}&captchaflag=login1&_login_image_={2}",
                                                  _userId, _userPassword, jpgcode);
                    var request = _httpHelper.CreateHTTPPOSTRequest(loginUrl4, postStr, true);
                    HTTPHelper.GetResponseString(request);
                }
                System.Threading.Thread.Sleep(1000);

                try
                {
                    var loginUrl5 = "http://zhjwxk.cic.tsinghua.edu.cn/xkBks.vxkBksXkbBs.do?m=main";
                    var request   = _httpHelper.CreateHTTPGetRequest(loginUrl5, true);
                    HTTPHelper.GetResponseString(request);
                }
                catch (Exception e)
                {
                    errorcount++;
                    OutputLog("登录错误,重新登录。错误信息:" + e.Message);
                    System.Threading.Thread.Sleep(2000);
                    continue;
                }
                login = true;
                OutputLog("登录成功");
            }
        }