Ejemplo n.º 1
0
        public void Login()
        {
            OutputLog("开始登录");

            {
                while (true)
                {
                    OutputLog("打开登录页面");
                    string loginUrl1 = "http://zhjwxk.cic.tsinghua.edu.cn/xsxk_index.jsp";
                    var    html      = HttpGet(loginUrl1);
                    if (!html.Contains("忙"))
                    {
                        break;
                    }

                    Sleep(300);
                }
            }

            {
                string loginUrl2 = "http://zhjwxk.cic.tsinghua.edu.cn/xklogin.do";
                HttpGet(loginUrl2);
            }

            int errorcount = 0;

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

                    if (jpgcode == null)
                    {
                        OutputLog("识别jpg失败");
                    }

                    Sleep(1000);
                }

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

                try
                {
                    bool loginIn = IsLoginIn();

                    if (loginIn)
                    {
                        OutputLog("登录成功");
                        break;
                    }
                    else
                    {
                        errorcount++;
                        OutputLog("登录失败,重新登录。");

                        Sleep(2000);
                    }
                }
                catch (Exception e)
                {
                    errorcount++;
                    OutputLog("登录过程中出现错误,重新登录。错误信息:" + e.Message);

                    Sleep(2000);
                }
            }
        }