Пример #1
0
        // Web interaction methods
        private bool PrepareLogin()
        {
            InsertLog("Waiting response from AIMS server...", Color.Black);

            WebManager loginPage = new WebManager("https://banweb.cityu.edu.hk/pls/PROD/twgkpswd_cityu.P_WWWLogin");

            try
            {
                loginPage.Load();
                InsertLog(loginPage.ToString());

                InsertLog("Server responsed.", Color.Blue);

                // Get required login parameters
                foreach (HtmlAgilityPack.HtmlAttribute attr in loginPage.GetHtmlNode("//input[@name='p_sess_id']").Attributes.AttributesWithName("value"))
                {
                    SessionID = attr.Value;
                }
                foreach (HtmlAgilityPack.HtmlAttribute attr in loginPage.GetHtmlNode("//input[@name='p_ip']").Attributes.AttributesWithName("value"))
                {
                    IPAddress = attr.Value;
                }

                InsertLog("p_ip: " + IPAddress + ", p_sess_id: " + SessionID, Color.Green);

                return(true);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                if (ex is WebException)
                {
                    InsertLog("Server returned error: " + ex.Message, Color.Red);
                }
                else if (ex is NullReferenceException)
                {
                    InsertLog("Cannot retrieve the login parameters.", Color.Red);
                }
                else
                {
                    InsertLog(ex.ToString(), Color.Red);
                }
            }

            return(false);
        }
Пример #2
0
        private List <Course> GetCourseListFromRegisteredRecords()
        {
            InsertLog("Retrieving your active registered course...", Color.Black);

            WebManager activeCoursePage = new WebManager("https://banweb.cityu.edu.hk/pls/PROD/hwskaddp_cityu.P_ChkReg");

            try
            {
                activeCoursePage.Load();
                InsertLog(activeCoursePage.ToString());

                String title = activeCoursePage.GetHtmlNode("//h2").InnerText;
                label_registeredCourse.Text = label_registeredCourse.Text.Replace(":", " for " + title.Replace("Active Registration (", "").Replace(")", "") + ":");

                List <Course> toReturn = new List <Course>();
                foreach (HtmlAgilityPack.HtmlNode row in activeCoursePage.GetHtmlNodeCollection("//table[3]//tr"))
                {
                    HtmlAgilityPack.HtmlNodeCollection courseDetails = row.SelectNodes("td");

                    // Ignore the first row
                    if (courseDetails != null && courseDetails[0].InnerText != "CRN")
                    {
                        Course course = new Course();

                        course.CRN        = courseDetails[0].InnerText;
                        course.CourseCode = courseDetails[1].InnerText;
                        course.Section    = courseDetails[2].InnerText;
                        course.Title      = courseDetails[3].InnerText;

                        course.Credit = Convert.ToSingle(courseDetails[4].InnerText);

                        toReturn.Add(course);
                    }
                }

                InsertLog("Retrieved registered course.", Color.Blue);

                return(toReturn);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);

                if (ex is WebException)
                {
                    InsertLog("Server returned error: " + ex.Message, Color.Red);

                    if (ex.Message == "Break in attempt" || ex.Message == "Session timeout")
                    {
                        if (PrepareLogin() && Login())
                        {
                            return(GetCourseListFromRegisteredRecords());
                        }
                    }
                }
                else if (ex is NullReferenceException)
                {
                    InsertLog("Cannot retrieve registered course.", Color.Red);
                }
                else
                {
                    InsertLog(ex.ToString(), Color.Red);
                }
            }

            return(null);
        }