首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

c#模拟登录验证码异常的有关问题,马下结。

2013-03-22 
c#模拟登录验证码错误的问题,急急急。。。。马上结。。public static CookieContainer CookieContainers new C

c#模拟登录验证码错误的问题,急急急。。。。马上结。。

public static CookieContainer CookieContainers = new CookieContainer();
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string username = textBox2.Text.Trim();
            string password = textBox3.Text.Trim();
            string code = textBox4.Text.Trim();
            string data="__VIEWSTATE=dDwtMTg3MTM5OTI5MTs7PhMbjAbINgvvHsvy%2FQdjcw3RMlRn&TextBox1="+username+"&TextBox2="+password+"&TextBox3="+code+"&RadioButtonList1=%D1%A7%C9%FA&Button1=&lbLanguage=";
            string loginurl = "http://xk4.suda.edu.cn/";

            getresponse(loginurl,data);
        }

        private void getresponse(string loginurl,string data)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginurl);
            req.KeepAlive = true;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = "User-AgentMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            req.Accept = "Accepttext/html, application/xhtml+xml, */*";
            req.Timeout = 50000;
            req.AllowAutoRedirect = true;
            req.CookieContainer = CookieContainers;
            
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] postBytes = encoding.GetBytes(data);
            req.ContentLength = postBytes.Length;
            Stream st = req.GetRequestStream();
            st.Write(postBytes, 0, postBytes.Length);
            st.Close();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resset = res.GetResponseStream();


            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            StreamReader sr = new StreamReader(resset, myEncoding);
            string str = sr.ReadToEnd();
            textBox1.Text = str;
            webBrowser1.DocumentText = textBox1.Text;
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            HttpWebRequest webr = (HttpWebRequest)WebRequest.Create("http://xk4.suda.edu.cn/CheckCode.aspx");
            HttpWebResponse res = (HttpWebResponse)webr.GetResponse();
            Stream stream=res.GetResponseStream();
            pictureBox1.Image = Image.FromStream(stream);
            stream.Close();
        


这之中的手动输入验证码怎么总是错误啊。是什么地方错了啊???总是显示输入的验证码不正确。但是明明输入的与取得的验证码相同的。。。。。到底是什么情况。马上结。。。。
[解决办法]
是session不同步,解决办法定义一个全局变量string cookie=string.Emtpy;

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://xk4.suda.edu.cn/CheckCode.aspx");
HttpWebResponse response = (HttpWebResponse)webr.GetResponse();
Stream stream=response.GetResponseStream();
pictureBox1.Image = Image.FromStream(stream);
stream.Close();
if (null != request)
    request.Abort();
if (null != response)
    response.Close();
cookie = response.Headers.Get("Set-Cookie");

private void getresponse(string loginurl,string data)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(loginurl);
            req.Headers[HttpRequestHeader.Cookie] = cookie;
            req.KeepAlive = true;
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.UserAgent = "User-Agent   Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
            req.Accept = "Accept  text/html, application/xhtml+xml, */*";


            req.Timeout = -1;
            req.AllowAutoRedirect = true;
            req.CookieContainer = CookieContainers;
             
            ASCIIEncoding encoding = new ASCIIEncoding();
            byte[] postBytes = encoding.GetBytes(data);
            req.ContentLength = postBytes.Length;
            Stream st = req.GetRequestStream();
            st.Write(postBytes, 0, postBytes.Length);
            st.Close();
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            Stream resset = res.GetResponseStream();
            Encoding myEncoding = Encoding.GetEncoding("gb2312");
            StreamReader sr = new StreamReader(resset, myEncoding);
            string str = sr.ReadToEnd();
            textBox1.Text = str;
            webBrowser1.DocumentText = textBox1.Text;
        }

热点排行