Ejemplo n.º 1
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //从主页获取__VIEWSTATE信息
            debug_output("开始重定向教务URL");
            var ns = new NetStream();

            ns.HttpGetAsync(main_page_url, url_locate_callback, null);
        }
Ejemplo n.º 2
0
        //获取选课列表
        private void get_public_course(bool get_mode = true)
        {
            var url = get_session_url() + "/xf_xsqxxxk.aspx?xh=" + username.Text + "&gnmkdm=N121103";

            var ns           = new NetStream();
            var header_param = new Parameters();

            header_param.Add("Referer", get_session_url() + "xs_main.aspx?xh=" + username.Text);
            if (get_mode)
            {
                //get模式下获取选课课表
                debug_output("初始化选课列表数据");
                ns.HttpGetAsync(url, get_public_course_callback, headerParam: header_param, state: get_mode);
            }
            else
            {
                //post模式下获取选课课表
                debug_output("获取全部选课信息");
                header_param.Add("Origin", get_origin_url());
                string course_time = "", course_name = "";
                Invoke(new ThreadStart(delegate
                {
                    course_time = courseTime.Text;
                    course_name = courseName.Text;
                }));

                var post_param = new Parameters();
                post_param.Add("__EVENTTARGET", "");
                post_param.Add("__EVENTARGUMENT", "");
                post_param.Add("__VIEWSTATE", __VIEWSTATE);
                post_param.Add("ddl_kcxz", "");
                post_param.Add("ddl_ywyl", "");
                post_param.Add("ddl_kcgs", "");
                post_param.Add("ddl_xqbs", 2);
                post_param.Add("ddl_sksj", "");
                post_param.Add("TextBox1", "");
                post_param.Add("dpkcmcGrid:txtChoosePage", 1);
                post_param.Add("dpkcmcGrid:txtPageSize", 200);
                var post_data = Encoding.UTF8.GetBytes(post_param.BuildQueryString());

                ns.HttpPostAsync(url, post_data.Length, (sender, e) =>
                {
                    try
                    {
                        sender.RequestStream.Write(post_data, 0, post_data.Length);
                        sender.HttpPostResponseAsync(get_public_course_callback, get_mode);
                    }
                    catch (Exception)
                    {
                        debug_output("查询选课列表失败,3s后重试");
                        sender.Close();
                        get_public_course(get_mode);
                    }
                }, state: get_mode, postContentType: NetStream.DEFAULT_CONTENT_TYPE_PARAM, headerParam: header_param);
            }
        }
Ejemplo n.º 3
0
        //将jw2005.scuteo.com通过302重定向到指定的ip地址
        private void url_locate_callback(NetStream sender, object e)
        {
            //失败重试
            if (sender.HTTP_Response == null || (int)sender.HTTP_Response.StatusCode > 400)
            {
                if (sender.HTTP_Response == null)
                {
                    debug_output("重定向出错,3s后重试");
                }
                else
                {
                    debug_output("重定向出错:服务器返回状态码 " + (int)sender.HTTP_Response.StatusCode + "(" + sender.HTTP_Response.StatusCode.ToString() + "),3s后重试");
                }
                Thread.Sleep(3000);
                var url = sender.HTTP_Request.RequestUri.ToString();
                sender.Close();

                var ns = new NetStream();
                ns.HttpGetAsync(main_page_url, url_locate_callback, null);
                return;
            }

            //将主页url更改成30X跳转后响应的url
            main_page_url = sender.HTTP_Response.ResponseUri.ToString();
            //获取主页面的html信息(gb2312编码)
            var str_main_page = sender.ReadResponseString(Encoding.Default);

            //关闭http请求
            sender.Close();

            //正则匹配,获取viewstate
            var match = Regex.Match(str_main_page, "<input\\stype=\"hidden\"\\sname=\"__VIEWSTATE\"\\svalue=\"(?<value>[^\"]*?)\"\\s*/>");

            if (match.Success)
            {
                __VIEWSTATE = match.Result("${value}");
            }
            else
            {
                debug_output("获取VIEWSTATE属性值失败,3s后重试");
                Thread.Sleep(3000);
                var ns = new NetStream();
                ns.HttpGetAsync(main_page_url, url_locate_callback, null);
                return;
            }

            //刷新验证码
            refresh_captcha();
            //登陆按钮可点(委托到主线程)
            Invoke(new ThreadStart(delegate
            {
                login.Enabled = true;
            }));
        }
Ejemplo n.º 4
0
        //刷新验证码
        private void refresh_captcha()
        {
            debug_output("获取验证码图片");
            var url = get_session_url() + "/CheckCode.aspx";

            var ns           = new NetStream();
            var header_param = new Parameters();

            header_param.Add("Origin", main_page_url);
            ns.HttpGetAsync(url, (_sender, _e) =>
            {
                var memstream    = new MemoryStream();
                int readed_bytes = 0;
                //从网络数据流转移到内存数据流中
                const int buffer_size = 4096;
                var buffer            = new byte[buffer_size];
                do
                {
                    readed_bytes = _sender.ResponseStream.Read(buffer, 0, buffer_size);
                    memstream.Write(buffer, 0, readed_bytes);
                } while (readed_bytes > 0);
                ns.Close();
                memstream.Seek(0, SeekOrigin.Begin);

                var img = Image.FromStream(memstream); //从内存流中创建图像
                //主线程委托
                Invoke(new ThreadStart(delegate
                {
                    captchaImg.Image = img;
                    if (ocr_wrapper != null)
                    {
                        capcha.Text = ocr_wrapper.OCR_Image(img);
                    }
                }));
            }, headerParam: header_param);
        }